package com.newenergy.wisehome.config;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import com.newenergy.light.Helper.BleDataHelper;
import com.newenergy.light.Helper.EncryUtil;
import com.newenergy.light.ble.AdvertiserManager;
import com.newenergy.wisehome.R;
import com.newenergy.wisehome.callback.SelectGroupListener;
import com.newenergy.wisehome.common.MyEvent;
import com.newenergy.wisehome.common.helper.LogHelper;
import com.newenergy.wisehome.common.helper.SPUtil;
import com.newenergy.wisehome.common.manager.MyThreadManager;
import com.newenergy.wisehome.data.bean.BLDeviceInfo;
import com.newenergy.wisehome.data.bean.BallLight;
import com.newenergy.wisehome.data.bean.DeviceCategory;
import com.newenergy.wisehome.data.bean.Family;
import com.newenergy.wisehome.data.bean.SecneGroup;
import com.newenergy.wisehome.data.ormlite.dao.BallLightDao;
import com.newenergy.wisehome.data.ormlite.dao.DeviceCategoryDao;
import com.newenergy.wisehome.data.ormlite.dao.FamilyDao;
import com.newenergy.wisehome.gateway.common.BLCommonUtils;
import com.newenergy.wisehome.gateway.manager.BLLocalFamilyManager;

import org.greenrobot.eventbus.EventBus;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.com.broadlink.sdk.BLLet;
import cn.com.broadlink.sdk.result.controller.BLPassthroughResult;

import static com.newenergy.wisehome.common.helper.ToastUtil.myToast;


/**
 * Created by jnl on 2017/9/28.
 */

public class AppManager {

    public static final String TAG = "LFX";

    public BallLight selectGroup;
    private BallLightDao mBallLightDao;
    public Family mCurrFamily;
    private SelectGroupListener mSelectGroupListener;
    private final AdvertiserManager mAdvertiserManager;
    private List<SecneGroup> mSecneGroups;
    private String mScenePowerCmd;
    private int mSecneGroupIndex;
    private boolean mIsSceneCMD;
    public BallLight mAllGroup;
    private DeviceCategoryDao mDeviceCategoryDao;
    public DeviceCategory mSelectDeviceCategory;
    private Context sContext;
    public int mGroupIndex = 0;
    public int mHEAIndex = 0;

    public BallLight mDefaultBallLight;
    public DeviceCategory mDefaultDeviceCategoryLight;
    public DeviceCategory mDefaultDeviceCategoryFanLamp;//默认风扇灯（圆盘）
    public DeviceCategory mDefaultDeviceCategoryDataFanLamp;//默认风扇灯（数据版）

    public DeviceCategory mDefaultDeviceCategoryFan;//默认风扇
    public DeviceCategory mDefaultDeviceCategoryHumidifier;//默认加湿器
    public DeviceCategory mDefaultDeviceCategoryBathHeater;//默认新浴霸

    public BLDeviceInfo mBLAllDeviceInfo;

    public boolean isStopTouch = true;

    public boolean isControlAll = false;
    public boolean isBLControlAll = false;

    //网关部分
    public String mLightSerial = "0001";
    public String mDid = "";
    public String mPassCmdType = "00";


    @SuppressLint("StaticFieldLeak")
    private static AppManager instance;
    private Toast mToast;


    /**
     * 获取单例
     *
     * @return
     */
    public static AppManager getInstance(Context context) {
        if (instance == null) {
            instance = new AppManager(context);
        }
        return instance;
    }


    private AppManager(Context context) {
        sContext = context.getApplicationContext();
        mBallLightDao = new BallLightDao(sContext);
        mDeviceCategoryDao = new DeviceCategoryDao(sContext);
        mAdvertiserManager = AdvertiserManager.getInstance();

        mDefaultBallLight = new BallLight("12345678", 1, "00", sContext.getString(R.string.default_device), "mo ren zu", "btn_balllight");
        mDefaultDeviceCategoryLight = mDefaultBallLight.covertToDeviceCategory();

        mDefaultDeviceCategoryFanLamp = new DeviceCategory("12345678", 3, "01", sContext.getString(R.string.default_fanlamp1), "btn_fanlamp", "030100010000", "03010001");
        mDefaultDeviceCategoryDataFanLamp = new DeviceCategory("12345678", 3, "01", sContext.getString(R.string.default_fanlamp2), "btn_fanlamp", "030200010000", "03020001");


        mDefaultDeviceCategoryFan = new DeviceCategory("12345678", 2, "01", sContext.getString(R.string.default_fan), "btn_fan", "020100010000", "02010001");
        mDefaultDeviceCategoryHumidifier = new DeviceCategory("12345678", 13, "01", sContext.getString(R.string.default_Humidifier), "btn_humidifier", "0D0100010000", "0D010001");
        mDefaultDeviceCategoryBathHeater = new DeviceCategory("12345678", 5, "01", sContext.getString(R.string.default_BathHeater), "btn_batnheater", "050200010000", "05020001");


        String currFamilyId = SPUtil.getString("FAMILY_ID", "12345678");

        //创建所有设备
        BallLight ballLight = new BallLight(currFamilyId, 1, "00", sContext.getString(R.string.all_device), "suo you deng", "bg_all_device_n");
        mAllGroup = ballLight;

        Family family = new FamilyDao(context).getById(currFamilyId);
        if (family != null) {
            mCurrFamily = family;

            mAdvertiserManager.mSeed = mCurrFamily.getSeed();
            mAdvertiserManager.mFeedBack = mCurrFamily.getFeedback();
            List<DeviceCategory> deviceCategorys = mDeviceCategoryDao.getFamilyDeviceCategorys(currFamilyId);
            if (deviceCategorys.size() > 0) {
                mSelectDeviceCategory = deviceCategorys.get(0);
            }
        }

    }


    public void notifyAddGroup(BallLight group) {

        String defaultFamilyId = "12345678";
        int seed = mCurrFamily.getSeed();
        int feedback = mCurrFamily.getFeedback();
        String cmd = intToHex(seed) + numToHexs(feedback);
        mPassCmdType = "F0";
        AdvertiserManager advertiserManager = AdvertiserManager.getInstance();
        advertiserManager.isBleAdvertise = true;
        commandCmd("05", defaultFamilyId, group.getGroupId(), group.getFamilyId(), cmd, 73, 4777);


        advertiserManager.isBleAdvertise = false;

    }


    public void notifyScenePower(List<SecneGroup> secneGroups, String cmd) {
        mIsSceneCMD = true;
        mSecneGroups = secneGroups;
        mScenePowerCmd = cmd;
        mSecneGroupIndex = 0;
        sendDelayedMsg(HANDLER_SECNE_POWER, 0);
    }

    private void cancelHandleTask(int what) {

        if (mHandler.hasMessages(what)) {
            LogHelper.i("取消場景任務");
            mHandler.removeMessages(what);
        }
    }

    private void secnePower() {
        if (mSecneGroups == null || mSecneGroups.size() < 1 ||
                mSecneGroupIndex >= mSecneGroups.size()) {
            mIsSceneCMD = false;
            cancelHandleTask(HANDLER_SECNE_POWER);
            return;
        }

        SecneGroup secneGroup = mSecneGroups.get(mSecneGroupIndex);
        LogHelper.i("secnePower secneGroup=" + secneGroup.toString());

        if (mScenePowerCmd.equals("01")) {
            String scenedata = secneGroup.getScenedata();
            onScene(scenedata);
        } else if (mScenePowerCmd.equals("00")) {
            commandSceneCmd("02", mCurrFamily.getFamilyId(), secneGroup.getGroupId(), "00000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback());

        }

        sendDelayedMsg(HANDLER_SECNE_POWER, 1000);
        mSecneGroupIndex++;
    }

    public void notifySpeechResult(String result) {
        notifySpeechPinYinResult(result);
    }


    public void remoteShareCmd(String type, String data1, int seed, int feedback, String deviceType) {
        mAdvertiserManager.isRemoteShare = true;
        commandCmdShare(type, data1, seed, feedback, deviceType);
        mAdvertiserManager.isRemoteShare = false;
    }


    /**
     * 语音识别-》文字转拼音命令
     *
     * @param content
     */
    public void notifySpeechPinYinResult(String content) {

        try {
            String chinese = getChinese(content);//先把数字的替换成中文
            String pinyinString = PinyinHelper.convertToPinyinString(chinese, " ", PinyinFormat.WITHOUT_TONE);
            Log.e("LFX:pinyinString=", pinyinString);

            if (mCurrFamily == null) {
                speackOK(R.raw.add_framily);
                return;
            }

            if (mSelectDeviceCategory == null) {
                speackOK(R.raw.add_group);
                return;
            }


            //判断语音是否包含组名
            checkGroup(pinyinString);

            if (pinyinString.contains("xuan ze") || pinyinString.contains("xuan zhong")) {//选择、选中
                if (!checkGroupAnother(pinyinString)) {
                    speackOK(R.raw.no_cmd);
                }
                return;
            }

            //控制球泡灯命令
            if (mSelectDeviceCategory.getCategory() == 1) {
                Log.e("LFX", "mSelectDeviceCategory==" + mSelectDeviceCategory.toString());
                controlBallLight(content, pinyinString);

                //控制风扇灯命令
            } else if (mSelectDeviceCategory.getModel().equals("03010001")) {
                controlFanLamp(content, pinyinString);

                //控制风扇灯数据命令
            } else if (mSelectDeviceCategory.getModel().equals("03020001")) {
                controlDataFanLamp(content, pinyinString);

                //控制红外风扇02010001命令
            } else if (mSelectDeviceCategory.getModel().equals("02010001")) {
                controlFan(pinyinString);

                //控制红外风扇02040001命令
            } else if (mSelectDeviceCategory.getModel().equals("02040001")) {
                controlSixFan(pinyinString);

                //控制电暖器命令
            } else if (mSelectDeviceCategory.getCategory() == 4) {
                controlElectricHeater(pinyinString);

                //控制浴霸命令
            } else if (mSelectDeviceCategory.getModel().equals("05010001")) {
                controlBathHeater(pinyinString);

                //控制浴霸命令(新的)
            } else if (mSelectDeviceCategory.getModel().equals("05020001")) {
                controlNewBathHeater(pinyinString);

                //控制养生盆命令
            } else if (mSelectDeviceCategory.getCategory() == 9) {
                controlHealthBasin(pinyinString);

                //控制加湿器命令
            } else if (mSelectDeviceCategory.getCategory() == 13) {
                controlHumidifier(content, pinyinString);
            }

        } catch (PinyinException e) {
            e.printStackTrace();
        }

    }

    private void controlDataFanLamp(String content, String pinyinString) {
        if (pinyinString.contains("kai deng") || pinyinString.contains("da kai deng") ||
                pinyinString.contains("guai deng") || pinyinString.contains("da guai deng")
                || pinyinString.contains("gai deng") || pinyinString.contains("da gai deng")
                || pinyinString.contains("tai deng")) {//开灯、打开灯、台灯
            speackOK(R.raw.ok);

            commandAdvertise("04", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "00000000", "000003", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

            return;

        } else if (pinyinString.contains("guan deng") ||
                pinyinString.contains("guan diao deng") ||
                pinyinString.contains("guan bi deng") ||
                pinyinString.contains("gan deng") ||
                pinyinString.contains("gan diao deng") ||
                pinyinString.contains("gan bi deng")) {//关灯、关掉灯、关闭灯
            speackOK(R.raw.ok);

            commandAdvertise("04", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "00000000", "000002", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

            return;

        } else if (pinyinString.contains("feng shan deng guan")
                || pinyinString.contains("feng shan deng jian")
                || pinyinString.contains("guan feng shan deng")
                || pinyinString.contains("guan bi feng shan deng")
                || pinyinString.contains("guan diao feng shan deng")
                || pinyinString.contains("feng shan deng gan")
                || pinyinString.contains("gan feng shan deng")
                || pinyinString.contains("feng san deng guan")
                || pinyinString.contains("feng san deng jian")
                || pinyinString.contains("guan feng san deng")
                || pinyinString.contains("feng san deng gan")
                || pinyinString.contains("gan feng san deng")
                || pinyinString.contains("feng shuan deng guan")
                || pinyinString.contains("feng shuan deng jian")
                || pinyinString.contains("guan feng shuan deng")
                || pinyinString.contains("feng shuan deng gan")
                || pinyinString.contains("gan feng shuan deng")
                || pinyinString.contains("quan kuai")
                || pinyinString.contains("kuai quan bu")
                || pinyinString.contains("kuai suo you")
                || pinyinString.contains("da kuai quan bu")
                || pinyinString.contains("da kuai suo you")
                ) {//风扇灯关
            speackOK(R.raw.ok);

            commandAdvertise("04", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "00000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");
            SPUtil.put(Constants.ISONDATAFANLAMP, false);
            return;

        } else if (pinyinString.contains("feng shan deng kai")
                || pinyinString.contains("kai feng shan deng")
                || pinyinString.contains("feng shan deng guai")
                || pinyinString.contains("guai feng shan deng")
                || pinyinString.contains("feng san deng kai")
                || pinyinString.contains("kai feng san deng")
                || pinyinString.contains("feng san deng guai")
                || pinyinString.contains("guai feng san deng")
                || pinyinString.contains("feng shuan deng kai")
                || pinyinString.contains("kai feng shuan deng")
                || pinyinString.contains("feng shuan deng guai")
                || pinyinString.contains("guai feng shuan deng") || pinyinString.contains("quan guan")
                || pinyinString.contains("guan quan bu")
                || pinyinString.contains("guan suo you")
                || pinyinString.contains("guan bi suo you")
                || pinyinString.contains("guan bi quan bu")
                || pinyinString.contains("guan diao quan bu")
                || pinyinString.contains("guan diao suo you")
                ) {//风扇灯开
            speackOK(R.raw.ok);

            commandAdvertise("04", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "00000000", "000001", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");
            SPUtil.put(Constants.ISONDATAFANLAMP, true);
            return;

        } else if (pinyinString.contains("feng shan guan")
                || pinyinString.contains("feng shan jian")
                || pinyinString.contains("guan feng shan")
                || pinyinString.contains("feng shan gan")
                || pinyinString.contains("gan feng shan")
                || pinyinString.contains("feng san guan")
                || pinyinString.contains("feng san jian")
                || pinyinString.contains("guan feng san")
                || pinyinString.contains("feng san gan")
                || pinyinString.contains("gan feng san")
                || pinyinString.contains("feng shuan guan")
                || pinyinString.contains("feng shuan jian")
                || pinyinString.contains("guan feng shuan")
                || pinyinString.contains("feng shuan gan")
                || pinyinString.contains("gan feng shuan")
                || pinyinString.contains("feng shan ting zhi")
                || pinyinString.contains("ting zhi feng shan")) {//风扇关、风扇停止
            speackOK(R.raw.ok);

            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "02000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

            return;

        } else if (pinyinString.contains("feng shan kai")
                || pinyinString.contains("kai feng shan")
                || pinyinString.contains("feng shan guai")
                || pinyinString.contains("guai feng shan")
                || pinyinString.contains("feng san kai")
                || pinyinString.contains("kai feng san")
                || pinyinString.contains("feng san guai")
                || pinyinString.contains("guai feng san")
                || pinyinString.contains("feng shuan kai")
                || pinyinString.contains("kai feng shuan")
                || pinyinString.contains("feng shuan guai")
                || pinyinString.contains("guai feng shuan")
                || pinyinString.contains("feng shan qi dong")
                || pinyinString.contains("qi dong feng shan")
                || pinyinString.contains("kai qi feng shan")
                || pinyinString.contains("feng shan kai qi")
                || pinyinString.contains("feng shan zhuan")) {//风扇开、风扇启动，风扇转
            speackOK(R.raw.ok);

            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "07000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

            return;

        } else if (pinyinString.contains("ding shi yi xiao shi")
                || pinyinString.contains("bing shi yi xiao shi")
                || pinyinString.contains("dian shi yi xiao shi")
                || pinyinString.contains("deng shi yi xiao shi")
                || pinyinString.contains("ding si yi xiao si")
                || pinyinString.contains("bing si yi xiao si")
                || pinyinString.contains("dian si yi xiao si")
                || pinyinString.contains("deng si yi xiao si")
                || content.contains("定时1小时")) {//定时一小时
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "03000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

            return;

        } else if (pinyinString.contains("ding shi liang xiao shi")
                || pinyinString.contains("bing shi liang xiao shi")
                || pinyinString.contains("dian shi liang xiao shi")
                || pinyinString.contains("ding shi er xiao shi")
                || pinyinString.contains("deng shi er xiao shi")
                || pinyinString.contains("deng shi liang xiao shi")
                || pinyinString.contains("ding si liang xiao si")
                || pinyinString.contains("bing si liang xiao si")
                || pinyinString.contains("dian si liang xiao si")
                || pinyinString.contains("ding si er xiao si")
                || pinyinString.contains("deng si er xiao si")
                || pinyinString.contains("deng si liang xiao si")
                || content.contains("定时2小时")
                || content.contains("定时二小时")
                ) {//定时两小时
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "04000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

            return;

        } else if (pinyinString.contains("ding shi si xiao shi")
                || pinyinString.contains("bing shi si xiao shi")
                || pinyinString.contains("dian shi si xiao shi")
                || pinyinString.contains("deng shi si xiao shi")
                || pinyinString.contains("ding si si xiao si")
                || pinyinString.contains("bing si si xiao si")
                || pinyinString.contains("dian si si xiao si")
                || pinyinString.contains("deng si si xiao si")
                || content.contains("定时4小时")
                || content.contains("定时四小时")
                ) {//定时四小时
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "05000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

            return;

        } else if (pinyinString.contains("man") || pinyinString.contains("di su")) {//慢
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "06000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");
            return;

        } else if (pinyinString.contains("zhong xin")
                || pinyinString.contains("zong xin")
                || pinyinString.contains("zhong sheng")) {//饮食修改为中性光
            mSelectDeviceCategory.setSw(127);
            mSelectDeviceCategory.setLd(235);
            DeviceCategoryDao deviceCategoryDao = new DeviceCategoryDao(sContext);
            deviceCategoryDao.updateDeviceCategory(mSelectDeviceCategory);
            speackOK(R.raw.ok);
            commandAdvertise("02", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "ffff0000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

            return;
        } else if (pinyinString.contains("zhong") || pinyinString.contains("zong")) {//中
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "07000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");
            return;

        } else if (pinyinString.contains("kuai")
                || pinyinString.contains("kai su")
                || pinyinString.contains("gao su")) {//快、高速
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "08000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");
            return;

        } else if (pinyinString.contains("se wen jian") || pinyinString.contains("se wen di")
                || pinyinString.contains("she wen jian") || pinyinString.contains("she wen di")
                || pinyinString.contains("ce wen jian") || pinyinString.contains("ce wen di")
                || pinyinString.contains("se wen gan") || pinyinString.contains("she wen gan")
                || pinyinString.contains("ce wen gan")
                || pinyinString.contains("huang yi di") || pinyinString.contains("huang dian")
                || pinyinString.contains("zai huang") || pinyinString.contains("bian huang")
                || pinyinString.contains("geng huang") || pinyinString.contains("huang yi ｄ")
                || pinyinString.contains("nuan yi di") || pinyinString.contains("nuan dian")
                || pinyinString.contains("zai nuan") || pinyinString.contains("bian nuan")
                || pinyinString.contains("geng nuan") || pinyinString.contains("nuan yi ｄ")
                || pinyinString.contains("wang yi di") || pinyinString.contains("wang dian")
                || pinyinString.contains("zai wang") || pinyinString.contains("bian wang")
                || pinyinString.contains("geng wang") || pinyinString.contains("wang yi ｄ")
                || pinyinString.contains("hang yi di") || pinyinString.contains("hang dian")
                || pinyinString.contains("zai hang") || pinyinString.contains("bian hang")
                || pinyinString.contains("geng hang") || pinyinString.contains("hang yi ｄ")
                || pinyinString.contains("se wen bian di") || pinyinString.contains("se wen de")
                || pinyinString.contains("se wen qiang") || content.contains("she wen de")) {//色温减小\减少\黄一点
            speackOK(R.raw.ok);

            sendPWMValue(2, 50);

            return;


        } else if (pinyinString.contains("se wen zeng") || pinyinString.contains("se wen gao")
                || pinyinString.contains("se wen jia") || pinyinString.contains("se wen shen")
                || pinyinString.contains("she wen zeng") || pinyinString.contains("she wen gao")
                || pinyinString.contains("she wen jia") || pinyinString.contains("she wen shen")
                || pinyinString.contains("ce wen zeng") || pinyinString.contains("ce wen gao")
                || pinyinString.contains("ce wen jia") || pinyinString.contains("ce wen shen")
                || pinyinString.contains("se wen zhen") || pinyinString.contains("she wen zhen")
                || pinyinString.contains("ce wen zhen") || pinyinString.contains("ce wen zheng")
                || pinyinString.contains("se wen zheng") || pinyinString.contains("she wen zheng")
                || pinyinString.contains("bai yi ｄ") || pinyinString.contains("bai yi di")
                || pinyinString.contains("bai yi die") || pinyinString.contains("bai yi dian")
                || pinyinString.contains("pai yi dian") || pinyinString.contains("pai yi die")
                || pinyinString.contains("pai yi ｄ") || pinyinString.contains("pai yi di")
                || pinyinString.contains("bai dian") || pinyinString.contains("bian bai")
                || pinyinString.contains("zai bai") || pinyinString.contains("leng yi dian")
                || pinyinString.contains("leng dian") || pinyinString.contains("bian leng")
                || pinyinString.contains("zai leng") || pinyinString.contains("geng leng")
                || pinyinString.contains("se wen bian gao")
                ) {//色温增加\增大\白一点
            speackOK(R.raw.ok);
            sendPWMValue(2, -50);
            return;


        } else if (pinyinString.contains("liang du jian") || pinyinString.contains("liang du di")
                || pinyinString.contains("liang du gan") || content.contains("liang du qiang")
                || pinyinString.contains("niang du jian") || pinyinString.contains("niang du di")
                || pinyinString.contains("niang du gan")
                || pinyinString.contains("lang du jian") || pinyinString.contains("lang du di")
                || pinyinString.contains("lang du gan")
                || pinyinString.contains("an yi ｄ")
                || pinyinString.contains("an yi di") || pinyinString.contains("yan yi di")
                || pinyinString.contains("yan １ dian") || pinyinString.contains("ying yi dian")
                || pinyinString.contains("an yi dian") || pinyinString.contains("an dian")
                || pinyinString.contains("zai an") || pinyinString.contains("geng an")
                || pinyinString.contains("bian an") || pinyinString.contains("hun dian")
                || pinyinString.contains("hun yi dian") || pinyinString.contains("zai hun")
                || pinyinString.contains("geng hun") || pinyinString.contains("bian hun")
                || content.contains("liang du bian di")
                ) {//亮度减小\减少、暗一点、昏一点
            speackOK(R.raw.ok);
            sendPWMValue(1, -50);

            return;
        } else if (pinyinString.contains("liang du zeng") || pinyinString.contains("liang du gao")
                || pinyinString.contains("liang du jia") || pinyinString.contains("liang du shen")
                || pinyinString.contains("liang yi ｄ") || pinyinString.contains("liang yi di")
                || pinyinString.contains("niang du zeng") || pinyinString.contains("niang du gao")
                || pinyinString.contains("niang du jia") || pinyinString.contains("niang du shen")
                || pinyinString.contains("niang yi ｄ") || pinyinString.contains("niang yi di")
                || pinyinString.contains("lang du zeng") || pinyinString.contains("lang du gao")
                || pinyinString.contains("lang du jia") || pinyinString.contains("lang du shen")
                || pinyinString.contains("lang yi ｄ") || pinyinString.contains("liang yi di")
                || pinyinString.contains("ming yi dian")
                || pinyinString.contains("liang dian") || pinyinString.contains("zai liang")
                || pinyinString.contains("geng liang") || pinyinString.contains("bian liang")
                || pinyinString.contains("niang dian") || pinyinString.contains("zai niang")
                || pinyinString.contains("geng niang") || pinyinString.contains("bian niang")
                || pinyinString.contains("lang dian") || pinyinString.contains("zai lang")
                || pinyinString.contains("geng lang") || pinyinString.contains("bian lang")
                ) {//亮度增加\增大\亮一点、明一点
            speackOK(R.raw.ok);
            sendPWMValue(1, 50);
            return;

        } else if (pinyinString.contains("huang gua") || pinyinString.contains("nuan gua") || pinyinString.contains("nuan se")) {//阳光修改为黄光、暖光、暖色
            mSelectDeviceCategory.setSw(255);
            mSelectDeviceCategory.setLd(235);
            DeviceCategoryDao deviceCategoryDao = new DeviceCategoryDao(sContext);
            deviceCategoryDao.updateDeviceCategory(mSelectDeviceCategory);
            speackOK(R.raw.ok);
            commandAdvertise("02", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "ff000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

            return;
        } else if (pinyinString.contains("bai guang") || pinyinString.contains("bai gang") || pinyinString.contains("leng gua")) {//纯白改成白光、冷光
            mSelectDeviceCategory.setSw(0);
            mSelectDeviceCategory.setLd(235);
            DeviceCategoryDao deviceCategoryDao = new DeviceCategoryDao(sContext);
            deviceCategoryDao.updateDeviceCategory(mSelectDeviceCategory);
            speackOK(R.raw.ok);
            commandAdvertise("02", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "00ff0000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

            return;
        } else if (pinyinString.contains("xiao ye deng") || pinyinString.contains("ye guang")) {//小夜灯、夜光
            mSelectDeviceCategory.setSw(127);
            mSelectDeviceCategory.setLd(0);
            DeviceCategoryDao deviceCategoryDao = new DeviceCategoryDao(sContext);
            deviceCategoryDao.updateDeviceCategory(mSelectDeviceCategory);
            speackOK(R.raw.ok);
            commandAdvertise("02", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "14140000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

            return;
        } else if (pinyinString.contains("fen duan") || pinyinString.contains("fen dan")) {//分段
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "0D000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");
            return;
        } else if (pinyinString.contains("fu guang") || pinyinString.contains("shu guang")
                || pinyinString.contains("pu guang") || pinyinString.contains("fo guang")
                || pinyinString.contains("fu gang") || pinyinString.contains("shu gang")
                || pinyinString.contains("pu gang") || pinyinString.contains("fo gang")) {//辅光
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "0E000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

            return;
        } else if (pinyinString.contains("rou he")
                || pinyinString.contains("liu he")
                || pinyinString.contains("you he")
                || pinyinString.contains("ru he")) {//柔和
            mSelectDeviceCategory.setSw(127);
            mSelectDeviceCategory.setLd(0);
            DeviceCategoryDao deviceCategoryDao = new DeviceCategoryDao(sContext);
            deviceCategoryDao.updateDeviceCategory(mSelectDeviceCategory);
            speackOK(R.raw.ok);
            commandAdvertise("02", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "7f7f0000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");


            return;
        } else if (pinyinString.contains("zui a") || pinyinString.contains("zui yan")
                || pinyinString.contains("zui xiao") || pinyinString.contains("dui a")) {//最暗、最爱、最小、对岸
            speackOK(R.raw.ok);
            sendPWMValue(1, -235);

            return;
        } else if (pinyinString.contains("zui liang") || pinyinString.contains("zui niang")
                || pinyinString.contains("zui lang") || pinyinString.contains("zui da")
                || pinyinString.contains("dui liang")) {//最亮\最大、对亮
            speackOK(R.raw.ok);
            sendPWMValue(1, 235);

            return;
        } else if (pinyinString.contains("zui bai")
                || pinyinString.contains("zui pai")
                || pinyinString.contains("dui bai")
                ) {//最白
            speackOK(R.raw.ok);
            sendPWMValue(2, -255);

            return;
        } else if (pinyinString.contains("zui huang")
                || pinyinString.contains("zui wang")
                || pinyinString.contains("zui hang")
                || pinyinString.contains("dui huang")
                ) {//最黄、
            speackOK(R.raw.ok);
            sendPWMValue(2, 255);

            return;
        } else if ((pinyinString.contains("liang") || pinyinString.contains("ming"))) {//亮、明
            speackOK(R.raw.ok);
            sendPWMValue(1, 50);//调用亮一点命令

            return;
        } else if ((pinyinString.contains("huang") || pinyinString.contains("nuan"))) {//黃、暖
            speackOK(R.raw.ok);
            sendPWMValue(2, 50);//调用黃一点命令

            return;
        } else if ((pinyinString.contains("hun") || pinyinString.contains("an"))) {//昏、暗
            speackOK(R.raw.ok);
            sendPWMValue(1, -50);//调用暗一点的命令

            return;
        } else if ((pinyinString.contains("bai") || pinyinString.contains("leng"))) {//白、冷
            speackOK(R.raw.ok);
            sendPWMValue(2, -50);//调用白一点命令

            return;
        } else if ((pinyinString.contains("hei") || pinyinString.contains("mie"))) {//黑、灭
            speackOK(R.raw.ok);

            commandAdvertise("04", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "00000000", "000002", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");


            return;
        } else if (checkGroupAnother(pinyinString)) {
            speackOK(R.raw.ok);
            return;
        } else {
            speackOK(R.raw.no_cmd);
        }


    }

    private void controlHumidifier(String content, String pinyinString) {
        int key = 0;
        int fkey = 0;
        if (pinyinString.contains("heng shi") || pinyinString.contains("heng si")) {//恒湿
            if (pinyinString.contains("kai")) {
                speackOK(R.raw.ok);
                key = 0x61;
                fkey = (key ^ 0xff);
                String keyHex = getByteHex(key) + getByteHex(fkey);
                commandAdvertise("02", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "137F" + keyHex, "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "720D");
                return;
            } else if (pinyinString.contains("guan")) {
                speackOK(R.raw.ok);
                key = 0x62;
                fkey = (key ^ 0xff);
                String keyHex = getByteHex(key) + getByteHex(fkey);
                commandAdvertise("02", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "137F" + keyHex, "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "720D");
                return;
            }

        } else if (pinyinString.contains("shui mian")) {//睡眠
            if (pinyinString.contains("kai")) {
                speackOK(R.raw.ok);
                key = 0x51;
                fkey = (key ^ 0xff);
                String keyHex = getByteHex(key) + getByteHex(fkey);
                commandAdvertise("02", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "137F" + keyHex, "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "720D");
                return;
            } else if (pinyinString.contains("guan")) {
                speackOK(R.raw.ok);
                key = 0x52;
                fkey = (key ^ 0xff);
                String keyHex = getByteHex(key) + getByteHex(fkey);
                commandAdvertise("02", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "137F" + keyHex, "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "720D");
                return;
            }

        } else if (pinyinString.contains("ye deng")) {//夜灯
            if (pinyinString.contains("kai")) {
                speackOK(R.raw.ok);
                key = 0x71;
                fkey = (key ^ 0xff);
                String keyHex = getByteHex(key) + getByteHex(fkey);
                commandAdvertise("02", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "137F" + keyHex, "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "720D");
                return;
            } else if (pinyinString.contains("guan")) {
                speackOK(R.raw.ok);
                key = 0x72;
                fkey = (key ^ 0xff);
                String keyHex = getByteHex(key) + getByteHex(fkey);
                commandAdvertise("02", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "137F" + keyHex, "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "720D");
                return;
            }

        } else if (pinyinString.contains("ding shi")
                || pinyinString.contains("bing shi")
                || pinyinString.contains("dian shi")) {//定时

            if (pinyinString.contains("yi xiao shi")) {
                speackOK(R.raw.ok);
                key = 0x21;
                fkey = (key ^ 0xff);
                String keyHex = getByteHex(key) + getByteHex(fkey);
                commandAdvertise("02", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "137F" + keyHex, "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "720D");
                return;


            } else if (pinyinString.contains("liang xiao shi")
                    || pinyinString.contains("er xiao shi")
                    || content.contains("定时2小时")
                    || content.contains("定时二小时")) {
                speackOK(R.raw.ok);
                key = 0x22;
                fkey = (key ^ 0xff);
                String keyHex = getByteHex(key) + getByteHex(fkey);
                commandAdvertise("02", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "137F" + keyHex, "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "720D");
                return;

            } else if (pinyinString.contains("si xiao shi")
                    || content.contains("定时4小时")
                    || content.contains("定时四小时")) {
                speackOK(R.raw.ok);
                key = 0x24;
                fkey = (key ^ 0xff);
                String keyHex = getByteHex(key) + getByteHex(fkey);
                commandAdvertise("02", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "137F" + keyHex, "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "720D");
                return;

            } else if (pinyinString.contains("ba xiao shi")
                    || content.contains("定时8小时")
                    || content.contains("定时八小时")) {
                speackOK(R.raw.ok);
                key = 0x28;
                fkey = (key ^ 0xff);
                String keyHex = getByteHex(key) + getByteHex(fkey);
                commandAdvertise("02", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "137F" + keyHex, "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "720D");
                return;

            } else if (pinyinString.contains("qu xiao")) {
                speackOK(R.raw.ok);
                key = 0x20;
                fkey = (key ^ 0xff);
                String keyHex = getByteHex(key) + getByteHex(fkey);
                commandAdvertise("02", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "137F" + keyHex, "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "720D");
                return;

            }

        } else if (pinyinString.contains("wu liang yi dang") || pinyinString.contains("hu liang yi dang")
                || pinyinString.contains("yi dang")) {
            speackOK(R.raw.ok);
            key = 0x31;
            fkey = (key ^ 0xff);
            String keyHex = getByteHex(key) + getByteHex(fkey);
            commandAdvertise("02", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "137F" + keyHex, "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "720D");
            return;


        } else if (pinyinString.contains("wu liang liang dang") || pinyinString.contains("hu liang liang dang")
                || pinyinString.contains("liang dang")) {//两档
            speackOK(R.raw.ok);
            key = 0x32;
            fkey = (key ^ 0xff);
            String keyHex = getByteHex(key) + getByteHex(fkey);
            commandAdvertise("02", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "137F" + keyHex, "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "720D");
            return;

        } else if (pinyinString.contains("wu liang san dang") || pinyinString.contains("hu liang san dang")
                || pinyinString.contains("san dang") || pinyinString.contains("shan dang")) {//三档

            speackOK(R.raw.ok);
            key = 0x33;
            fkey = (key ^ 0xff);
            String keyHex = getByteHex(key) + getByteHex(fkey);
            commandAdvertise("02", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "137F" + keyHex, "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "720D");
            return;

        } else if (pinyinString.contains("wu liang si dang") || pinyinString.contains("hu liang si dang")
                || pinyinString.contains("si dang")) {
            speackOK(R.raw.ok);
            key = 0x34;
            fkey = (key ^ 0xff);
            String keyHex = getByteHex(key) + getByteHex(fkey);
            commandAdvertise("02", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "137F" + keyHex, "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "720D");
            return;

        } else if (pinyinString.contains("kai")) {//开
            speackOK(R.raw.ok);

            key = 0x41;
            fkey = (key ^ 0xff);
            String keyHex = getByteHex(key) + getByteHex(fkey);
            SPUtil.put(Constants.ISONHUMIDIFIER, false);
            commandAdvertise("02", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "137F" + keyHex, "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "720D");
            return;

        } else if (pinyinString.contains("guan")) {//关
            speackOK(R.raw.ok);
            key = 0x42;
            fkey = (key ^ 0xff);
            String keyHex = getByteHex(key) + getByteHex(fkey);
            SPUtil.put(Constants.ISONHUMIDIFIER, true);
            commandAdvertise("02", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "137F" + keyHex, "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "720D");
            return;
        } else if (checkGroupAnother(pinyinString)) {
            speackOK(R.raw.ok);
            return;
        } else {
            speackOK(R.raw.no_cmd);
        }


    }

    //分电开关
   /* private void controlSplitSwitch(String pinyinString) {
        if (pinyinString.contains("kai")) {//开
            speackOK(R.raw.ok);
            commandAdvertise("02",mSelectDeviceCategory.getFamilyId(),mSelectDeviceCategory.getGroupId(),"00000000","000100",mCurrFamily.getSeed(),mCurrFamily.getFeedback(),1,"720B");
            Log.e("LFX", "分电开");
            return;

        }else if (pinyinString.contains("guan")) {//关
            speackOK(R.raw.ok);
            commandAdvertise("02",mSelectDeviceCategory.getFamilyId(),mSelectDeviceCategory.getGroupId(),"00000000","000000",mCurrFamily.getSeed(),mCurrFamily.getFeedback(),1,"720B");
            Log.e("LFX", "分电关");
            return;
        } else if (pinyinString.contains("xia yi dang")) {
            commandAdvertise("01",mSelectDeviceCategory.getFamilyId(),mSelectDeviceCategory.getGroupId(),"00000000","000000",mCurrFamily.getSeed(),mCurrFamily.getFeedback(),1,"720B");
            Log.e("LFX", "分电下一档");
            return;
        }
    }*/


    //养生盆
    private void controlHealthBasin(String pinyinString) {

        if (pinyinString.contains("kai guan")
                || pinyinString.contains("kai jian")) {//开关

            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "01000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7209");
            return;

        } else if (pinyinString.contains("mo shi")) {//模式
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "02000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7209");
            return;

        } else if (pinyinString.contains("ding shi")
                || pinyinString.contains("bing shi")
                || pinyinString.contains("dian shi")) {//定时

            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "03000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7209");
            return;

        } else if (pinyinString.contains("bian pin")) {//变频

            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "04000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7209");
            return;

        } else if (pinyinString.contains("qi pao")) {//气泡
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "05000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7209");
            return;

        } else if (pinyinString.contains("an mo")
                || pinyinString.contains("si ba")
                || pinyinString.contains("shi ba")
                || pinyinString.contains("S P A")) {//按摩、SPA
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "06000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7209");
            return;

        } else if (pinyinString.contains("wen du")) {//温度
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "07000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7209");
            return;

        } else if (checkGroupAnother(pinyinString)) {
            speackOK(R.raw.ok);
            return;
        } else {
            speackOK(R.raw.no_cmd);
        }
    }

    //浴霸
    private void controlBathHeater(String pinyinString) {

        if (pinyinString.contains("kai guan")
                || pinyinString.contains("kai jian")
                || pinyinString.contains("tong suo")) {//开关/童锁
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "01000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7205");

            return;

        } else if (pinyinString.contains("zhao ming") || pinyinString.contains("liang deng")) {//照明、亮灯
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "02000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7205");
            return;

        } else if (pinyinString.contains("chui qi")
                || pinyinString.contains("cui qi")) {//吹气
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "03000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7205");

            return;

        } else if (pinyinString.contains("huan qi")) {//换气

            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "04000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7205");
            return;

        } else if (pinyinString.contains("yi dang")) {//一档
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "05000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7205");
            return;

        } else if (pinyinString.contains("er dang")
                || pinyinString.contains("liang dang")) {//二档
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "06000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7205");
            return;

        } else if (checkGroupAnother(pinyinString)) {
            speackOK(R.raw.ok);
            return;
        } else {
            speackOK(R.raw.no_cmd);
        }
    }


    //浴霸（新的）
    private void controlNewBathHeater(String pinyinString) {
        if (pinyinString.contains("zhao ming") || pinyinString.contains("deng")) {//照明、灯
            if (pinyinString.contains("kai")) {
                AppManager.getInstance(sContext).commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "02010000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7205");
                SPUtil.put(Constants.ISONNEWBATHHEATERLIGHT, true);
                speackOK(R.raw.ok);
            } else if (pinyinString.contains("guan")) {
                AppManager.getInstance(sContext).commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "02000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7205");

                SPUtil.put(Constants.ISONNEWBATHHEATERLIGHT, false);
                speackOK(R.raw.ok);
            } else {
                speackOK(R.raw.no_cmd);
            }


            return;


        } else if (pinyinString.contains("yao bai")) {//摇摆
            if (pinyinString.contains("kai")) {
                AppManager.getInstance(sContext).commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "07400000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7205");
                SPUtil.put(Constants.ISONNEWBATHHEATERSWING, true);

                speackOK(R.raw.ok);
            } else if (pinyinString.contains("guan")) {
                AppManager.getInstance(sContext).commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "07000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7205");
                SPUtil.put(Constants.ISONNEWBATHHEATERSWING, false);

                speackOK(R.raw.ok);
            } else {
                speackOK(R.raw.no_cmd);
            }

            return;


        } else if (pinyinString.contains("huan qi")) {//换气
            if (pinyinString.contains("kai")) {
                AppManager.getInstance(sContext).commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "04200000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7205");
                SPUtil.put(Constants.ISONNEWBATHHEATEREXAIR, true);

                speackOK(R.raw.ok);
            } else if (pinyinString.contains("guan")) {
                AppManager.getInstance(sContext).commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "04000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7205");
                SPUtil.put(Constants.ISONNEWBATHHEATEREXAIR, false);

                speackOK(R.raw.ok);
            } else {
                speackOK(R.raw.no_cmd);
            }

            return;


        } else if (pinyinString.contains("chui qi") || pinyinString.contains("cui qi")
                || pinyinString.contains("chui feng") || pinyinString.contains("cui feng")) {//吹风、吹气
            if (pinyinString.contains("kai")) {
                AppManager.getInstance(sContext).commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "03020000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7205");
                SPUtil.put(Constants.ISONNEWBATHHEATERBLOWAIR, true);

                speackOK(R.raw.ok);
            } else if (pinyinString.contains("guan")) {
                AppManager.getInstance(sContext).commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "03000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7205");
                SPUtil.put(Constants.ISONNEWBATHHEATERBLOWAIR, false);

                speackOK(R.raw.ok);
            } else {
                speackOK(R.raw.no_cmd);
            }

            return;


        } else if (pinyinString.contains("yi dang") || pinyinString.contains("qu nuan yi")) {//一档、取暖一
            if (pinyinString.contains("kai")) {
                AppManager.getInstance(sContext).commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "05040000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7205");
                SPUtil.put(Constants.ISONNEWBATHHEATERWARM1, true);

                speackOK(R.raw.ok);
            } else if (pinyinString.contains("guan")) {
                AppManager.getInstance(sContext).commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "05000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7205");
                SPUtil.put(Constants.ISONNEWBATHHEATERWARM1, false);

                speackOK(R.raw.ok);
            } else {
                speackOK(R.raw.no_cmd);
            }

            return;


        } else if (pinyinString.contains("er dang") || pinyinString.contains("liang dang")
                || pinyinString.contains("qu nuan er") || pinyinString.contains("qu nuan liang")) {//二档、取暖二

            if (pinyinString.contains("kai")) {
                AppManager.getInstance(sContext).commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "06100000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7205");
                SPUtil.put(Constants.ISONNEWBATHHEATERWARM2, true);

                speackOK(R.raw.ok);
            } else if (pinyinString.contains("guan")) {
                AppManager.getInstance(sContext).commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "06000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7205");
                SPUtil.put(Constants.ISONNEWBATHHEATERWARM2, false);

                speackOK(R.raw.ok);
            } else {
                speackOK(R.raw.no_cmd);
            }


            return;

        } else if (checkGroupAnother(pinyinString)) {
            speackOK(R.raw.ok);
            return;
        } else {
            speackOK(R.raw.no_cmd);
        }
    }


    //电暖器
    private void controlElectricHeater(String pinyinString) {

        if (pinyinString.contains("kai guan")
                || pinyinString.contains("kai jian")) {//开关
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "01000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7204");

            return;

        } else if (pinyinString.contains("gong neng")) {//功能
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "02000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7204");
            return;

        } else if (pinyinString.contains("ding shi")
                || pinyinString.contains("bing shi")
                || pinyinString.contains("dian shi")) {//定时
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "03000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7204");

            return;

        } else if (pinyinString.contains("zuo you yao bai")
                || pinyinString.contains("zuo you yao tou")
                || pinyinString.contains("zuo you you tou")
                || pinyinString.contains("zuo you you bai")
                || pinyinString.contains("zuo you you tao")
                || pinyinString.contains("zuo you yao tiao")
                || pinyinString.contains("zuo yao you bai")
                || pinyinString.contains("yao tou")
                || pinyinString.contains("yao tao")
                || pinyinString.contains("you tou")
                || pinyinString.contains("you tao")
                || pinyinString.contains("you tou")
                || pinyinString.contains("yao bai")) {//摇头

            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "04000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7204");
            return;

        } else if (pinyinString.contains("zeng da")
                || pinyinString.contains("zeng jia")) {//温度增加
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "05000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7204");
            return;

        } else if (pinyinString.contains("jian xiao")
                || pinyinString.contains("jian shao")
                || pinyinString.contains("gan xiao")
                || pinyinString.contains("gan shao")) {//温度减少
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "06000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7204");
            return;

        } else if (checkGroupAnother(pinyinString)) {
            speackOK(R.raw.ok);
            return;
        } else {
            speackOK(R.raw.no_cmd);
        }
    }

    //风扇
    private void controlFan(String pinyinString) {

        if (pinyinString.contains("feng shan kai guan")
                || pinyinString.contains("feng shan kai jian")
                || pinyinString.contains("kai guan")) {//风扇开关
            speackOK(R.raw.ok);

            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "01000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7202");

            return;

        } else if (pinyinString.contains("feng su") || pinyinString.contains("feng shu") || pinyinString.contains("fen shu") || pinyinString.contains("fen su")) {//风速
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "05000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7202");
            return;

        } else if (pinyinString.contains("feng lei")
                || pinyinString.contains("fen lei")) {//风类
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "02000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7202");
            return;

        } else if (pinyinString.contains("zuo you yao bai")
                || pinyinString.contains("zuo you yao tou")
                || pinyinString.contains("zuo you you tou")
                || pinyinString.contains("zuo you you bai")
                || pinyinString.contains("zuo you you tao")
                || pinyinString.contains("zuo you yao tiao")
                || pinyinString.contains("zuo yao you bai")
                || pinyinString.contains("yao tou")
                || pinyinString.contains("yao tao")
                || pinyinString.contains("you tou")
                || pinyinString.contains("you tao")
                || pinyinString.contains("you tou")
                || pinyinString.contains("yao bai")) {//摇头

            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "04000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7202");
            return;

        } else if (pinyinString.contains("ding shi")
                || pinyinString.contains("bing shi")
                || pinyinString.contains("dian shi")) {//定时
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "03000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7202");

            return;

        } else if (checkGroupAnother(pinyinString)) {
            speackOK(R.raw.ok);
            return;
        } else {
            speackOK(R.raw.no_cmd);
        }
    }


    //风扇
    private void controlSixFan(String pinyinString) {

        if (pinyinString.contains("feng shan guan")
                || pinyinString.contains("feng shan jian")
                || pinyinString.contains("guan feng shan")
                || pinyinString.contains("feng shan gan")
                || pinyinString.contains("gan feng shan")
                || pinyinString.contains("feng san guan")
                || pinyinString.contains("feng san jian")
                || pinyinString.contains("guan feng san")
                || pinyinString.contains("feng san gan")
                || pinyinString.contains("gan feng san")
                || pinyinString.contains("feng shuan guan")
                || pinyinString.contains("feng shuan jian")
                || pinyinString.contains("guan feng shuan")
                || pinyinString.contains("feng shuan gan")
                || pinyinString.contains("gan feng shuan")) {//风扇关
            speackOK(R.raw.ok);

            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "01000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7202");

            return;

        } else if (pinyinString.contains("feng shan kai")
                || pinyinString.contains("kai feng shan")
                || pinyinString.contains("feng shan guai")
                || pinyinString.contains("guai feng shan")
                || pinyinString.contains("feng san kai")
                || pinyinString.contains("kai feng san")
                || pinyinString.contains("feng san guai")
                || pinyinString.contains("guai feng san")
                || pinyinString.contains("feng shuan kai")
                || pinyinString.contains("kai feng shuan")
                || pinyinString.contains("feng shuan guai")
                || pinyinString.contains("guai feng shuan")) {//风扇开
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "05000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7202");

            return;

        } else if (pinyinString.contains("feng su") || pinyinString.contains("feng shu") || pinyinString.contains("fen shu") || pinyinString.contains("fen su")) {//风速
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "05000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7202");
            return;

        } else if (pinyinString.contains("feng lei")
                || pinyinString.contains("fen lei")) {//风类
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "02000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7202");
            return;

        } else if (pinyinString.contains("zuo you yao bai")
                || pinyinString.contains("zuo you yao tou")
                || pinyinString.contains("zuo you you tou")
                || pinyinString.contains("zuo you you bai")
                || pinyinString.contains("zuo you you tao")
                || pinyinString.contains("zuo you yao tiao")
                || pinyinString.contains("zuo yao you bai")
                || pinyinString.contains("yao tou")
                || pinyinString.contains("yao tao")
                || pinyinString.contains("you tou")
                || pinyinString.contains("you tao")
                || pinyinString.contains("you tou")
                || pinyinString.contains("yao bai")) {//摇头

            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "04000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7202");
            return;

        } else if (pinyinString.contains("ding shi")
                || pinyinString.contains("bing shi")
                || pinyinString.contains("dian shi")) {//定时
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "03000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7202");

            return;

        } else if (pinyinString.contains("cai deng")) {//彩灯
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "06000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7202");

            return;

        } else if (checkGroupAnother(pinyinString)) {
            speackOK(R.raw.ok);
            return;
        } else {
            speackOK(R.raw.no_cmd);
        }
    }

    //风扇灯
    private void controlFanLamp(String content, String pinyinString) {
        if (pinyinString.contains("deng kai guan")
                || pinyinString.contains("deng kai jian")
                || pinyinString.contains("deng kai gan")
                || pinyinString.contains("deng guai guan")
                || pinyinString.contains("deng guai gan")
                || pinyinString.contains("deng guai jian")
                ) {//灯开关
            speackOK(R.raw.ok);

            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "01000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

            return;

        } else if (pinyinString.contains("feng shan guan")
                || pinyinString.contains("feng shan jian")
                || pinyinString.contains("guan feng shan")
                || pinyinString.contains("feng shan gan")
                || pinyinString.contains("gan feng shan")
                || pinyinString.contains("feng san guan")
                || pinyinString.contains("feng san jian")
                || pinyinString.contains("guan feng san")
                || pinyinString.contains("feng san gan")
                || pinyinString.contains("gan feng san")
                || pinyinString.contains("feng shuan guan")
                || pinyinString.contains("feng shuan jian")
                || pinyinString.contains("guan feng shuan")
                || pinyinString.contains("feng shuan gan")
                || pinyinString.contains("gan feng shuan")
                || pinyinString.contains("feng shan ting zhi")
                ) {//风扇关、风扇停止
            speackOK(R.raw.ok);

            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "02000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

            return;

        } else if (pinyinString.contains("feng shan kai")
                || pinyinString.contains("kai feng shan")
                || pinyinString.contains("feng shan guai")
                || pinyinString.contains("guai feng shan")
                || pinyinString.contains("feng san kai")
                || pinyinString.contains("kai feng san")
                || pinyinString.contains("feng san guai")
                || pinyinString.contains("guai feng san")
                || pinyinString.contains("feng shuan kai")
                || pinyinString.contains("kai feng shuan")
                || pinyinString.contains("feng shuan guai")
                || pinyinString.contains("guai feng shuan")
                || pinyinString.contains("feng shan qi dong")
                || pinyinString.contains("qi dong feng shan")
                || pinyinString.contains("kai qi feng shan")
                || pinyinString.contains("feng shan kai qi")
                || pinyinString.contains("feng shan zhuan")) {//风扇开、风扇启动

            speackOK(R.raw.ok);

            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "07000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

            return;

        } else if (pinyinString.contains("ding shi yi xiao shi")
                || pinyinString.contains("bing shi yi xiao shi")
                || pinyinString.contains("dian shi yi xiao shi")
                || pinyinString.contains("deng shi yi xiao shi")
                || pinyinString.contains("ding si yi xiao si")
                || pinyinString.contains("bing si yi xiao si")
                || pinyinString.contains("dian si yi xiao si")
                || pinyinString.contains("deng si yi xiao si")
                || content.contains("定时1小时")) {//定时一小时
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "03000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

            return;

        } else if (pinyinString.contains("ding shi liang xiao shi")
                || pinyinString.contains("bing shi liang xiao shi")
                || pinyinString.contains("dian shi liang xiao shi")
                || pinyinString.contains("ding shi er xiao shi")
                || pinyinString.contains("deng shi er xiao shi")
                || pinyinString.contains("deng shi liang xiao shi")
                || pinyinString.contains("ding si liang xiao si")
                || pinyinString.contains("bing si liang xiao si")
                || pinyinString.contains("dian si liang xiao si")
                || pinyinString.contains("ding si er xiao si")
                || pinyinString.contains("deng si er xiao si")
                || pinyinString.contains("deng si liang xiao si")
                || content.contains("定时2小时")
                || content.contains("定时二小时")
                ) {//定时两小时
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "04000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

            return;

        } else if (pinyinString.contains("ding shi si xiao shi")
                || pinyinString.contains("bing shi si xiao shi")
                || pinyinString.contains("dian shi si xiao shi")
                || pinyinString.contains("deng shi si xiao shi")
                || pinyinString.contains("ding si si xiao si")
                || pinyinString.contains("bing si si xiao si")
                || pinyinString.contains("dian si si xiao si")
                || pinyinString.contains("deng si si xiao si")
                || content.contains("定时4小时")
                || content.contains("定时四小时")
                ) {//定时四小时
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "05000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

            return;

        } else if (pinyinString.contains("man") || pinyinString.contains("di su")) {//慢
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "06000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");
            return;

        } else if (pinyinString.contains("zhong") || pinyinString.contains("zong")) {//中
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "07000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");
            return;

        } else if (pinyinString.contains("kuai")
                || pinyinString.contains("kai su")
                || pinyinString.contains("gao su")) {//快、高速
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "08000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");
            return;


        } else if (pinyinString.contains("se wen jian") || pinyinString.contains("se wen di")
                || pinyinString.contains("she wen jian") || pinyinString.contains("she wen di")
                || pinyinString.contains("ce wen jian") || pinyinString.contains("ce wen di")
                || pinyinString.contains("se wen gan") || pinyinString.contains("she wen gan")
                || pinyinString.contains("ce wen gan")
                || pinyinString.contains("huang yi di") || pinyinString.contains("huang dian")
                || pinyinString.contains("zai huang") || pinyinString.contains("bian huang")
                || pinyinString.contains("geng huang") || pinyinString.contains("huang yi ｄ")
                || pinyinString.contains("nuan yi di") || pinyinString.contains("nuan dian")
                || pinyinString.contains("zai nuan") || pinyinString.contains("bian nuan")
                || pinyinString.contains("geng nuan") || pinyinString.contains("nuan yi ｄ")
                || pinyinString.contains("wang yi di") || pinyinString.contains("wang dian")
                || pinyinString.contains("zai wang") || pinyinString.contains("bian wang")
                || pinyinString.contains("geng wang") || pinyinString.contains("wang yi ｄ")
                || pinyinString.contains("hang yi di") || pinyinString.contains("hang dian")
                || pinyinString.contains("zai hang") || pinyinString.contains("bian hang")
                || pinyinString.contains("geng hang") || pinyinString.contains("hang yi ｄ")
                || pinyinString.contains("se wen bian di") || pinyinString.contains("se wen de")
                || pinyinString.contains("se wen qiang") || content.contains("she wen de")
                ) {//色温减小\减少\黄一点、暖一点
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "09000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");
            return;

        } else if (pinyinString.contains("se wen zeng") || pinyinString.contains("se wen gao")
                || pinyinString.contains("se wen jia") || pinyinString.contains("se wen shen")
                || pinyinString.contains("she wen zeng") || pinyinString.contains("she wen gao")
                || pinyinString.contains("she wen jia") || pinyinString.contains("she wen shen")
                || pinyinString.contains("ce wen zeng") || pinyinString.contains("ce wen gao")
                || pinyinString.contains("ce wen jia") || pinyinString.contains("ce wen shen")
                || pinyinString.contains("se wen zhen") || pinyinString.contains("she wen zhen")
                || pinyinString.contains("ce wen zhen") || pinyinString.contains("ce wen zheng")
                || pinyinString.contains("se wen zheng") || pinyinString.contains("she wen zheng")
                || pinyinString.contains("bai yi ｄ") || pinyinString.contains("bai yi di")
                || pinyinString.contains("bai yi die") || pinyinString.contains("bai yi dian")
                || pinyinString.contains("pai yi dian") || pinyinString.contains("pai yi die")
                || pinyinString.contains("pai yi ｄ") || pinyinString.contains("pai yi di")
                || pinyinString.contains("bai dian") || pinyinString.contains("bian bai")
                || pinyinString.contains("zai bai") || pinyinString.contains("leng yi dian")
                || pinyinString.contains("leng dian") || pinyinString.contains("bian leng")
                || pinyinString.contains("zai leng") || pinyinString.contains("geng leng")
                || pinyinString.contains("se wen bian gao")
                ) {//色温增加\增大\白一点、冷一点
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "0A000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");
            return;

        } else if (pinyinString.contains("liang du jian") || pinyinString.contains("liang du di")
                || pinyinString.contains("liang du gan") || content.contains("liang du qiang")
                || pinyinString.contains("niang du jian") || pinyinString.contains("niang du di")
                || pinyinString.contains("niang du gan")
                || pinyinString.contains("lang du jian") || pinyinString.contains("lang du di")
                || pinyinString.contains("lang du gan")
                || pinyinString.contains("an yi ｄ")
                || pinyinString.contains("an yi di") || pinyinString.contains("yan yi di")
                || pinyinString.contains("yan １ dian") || pinyinString.contains("ying yi dian")
                || pinyinString.contains("an yi dian") || pinyinString.contains("an dian")
                || pinyinString.contains("zai an") || pinyinString.contains("geng an")
                || pinyinString.contains("bian an") || pinyinString.contains("hun dian")
                || pinyinString.contains("hun yi dian") || pinyinString.contains("zai hun")
                || pinyinString.contains("geng hun") || pinyinString.contains("bian hun")
                || content.contains("liang du bian di")
                ) {//亮度减小\减少、暗一点、昏一点
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "0C000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");
            return;
        } else if (pinyinString.contains("liang du zeng") || pinyinString.contains("liang du gao")
                || pinyinString.contains("liang du jia") || pinyinString.contains("liang du shen")
                || pinyinString.contains("liang yi ｄ") || pinyinString.contains("liang yi di")
                || pinyinString.contains("niang du zeng") || pinyinString.contains("niang du gao")
                || pinyinString.contains("niang du jia") || pinyinString.contains("niang du shen")
                || pinyinString.contains("niang yi ｄ") || pinyinString.contains("niang yi di")
                || pinyinString.contains("lang du zeng") || pinyinString.contains("lang du gao")
                || pinyinString.contains("lang du jia") || pinyinString.contains("lang du shen")
                || pinyinString.contains("lang yi ｄ") || pinyinString.contains("liang yi di")
                || pinyinString.contains("ming yi dian")
                || pinyinString.contains("liang dian") || pinyinString.contains("zai liang")
                || pinyinString.contains("geng liang") || pinyinString.contains("bian liang")
                || pinyinString.contains("niang dian") || pinyinString.contains("zai niang")
                || pinyinString.contains("geng niang") || pinyinString.contains("bian niang")
                || pinyinString.contains("lang dian") || pinyinString.contains("zai lang")
                || pinyinString.contains("geng lang") || pinyinString.contains("bian lang")
                ) {//亮度增加\增大\亮一点、明一点
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "0B000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");
            return;

        } else if (pinyinString.contains("fen duan") || pinyinString.contains("fen dan")) {//分段
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "0D000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");
            return;
        } else if (pinyinString.contains("fu guang") || pinyinString.contains("shu guang")
                || pinyinString.contains("pu guang") || pinyinString.contains("fo guang")
                || pinyinString.contains("fu gang") || pinyinString.contains("shu gang")
                || pinyinString.contains("pu gang") || pinyinString.contains("fo gang")) {//辅光
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "0E000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");
            return;
        } else if ((pinyinString.contains("liang") || pinyinString.contains("ming"))) {//亮、明
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "0B000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");
            //调用亮一点命令

            return;
        } else if ((pinyinString.contains("huang") || pinyinString.contains("nuan"))) {//黃、暖
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "09000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");
            //调用黃一点命令

            return;
        } else if ((pinyinString.contains("hun") || pinyinString.contains("an"))) {//昏、暗
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "0C000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");
            //调用暗一点的命令

            return;
        } else if ((pinyinString.contains("bai") || pinyinString.contains("leng"))) {//白、冷
            speackOK(R.raw.ok);
            commandAdvertise("01", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), "0A000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");
            //调用白一点命令

            return;
        } else if (checkGroupAnother(pinyinString)) {
            speackOK(R.raw.ok);
            return;
        } else {
            speackOK(R.raw.no_cmd);
        }
    }

    //球泡灯
    private void controlBallLight(String content, String pinyinString) {
        if (mSelectDeviceCategory.getFamilyId().equalsIgnoreCase("12345678")) {
            if (!isControlAll) {
                selectGroup = mDefaultBallLight;
            } else {
                selectGroup = mAllGroup;
            }


        } else {
            if (isControlAll) {
                selectGroup = mAllGroup;
            }

        }

        Log.e("LFX", "mSelectDeviceCategory==" + mSelectDeviceCategory.toString());

        if (pinyinString.contains("quan kuai") ||
                pinyinString.contains("quan gai") ||
                pinyinString.contains("quan kai") ||
                pinyinString.contains("quan guai") ||
                pinyinString.contains("kuai quan bu") ||
                pinyinString.contains("kuai suo you") ||
                pinyinString.contains("kai quan bu") ||
                pinyinString.contains("kai suo you") ||
                pinyinString.contains("gai quan bu") ||
                pinyinString.contains("gai suo you") ||
                pinyinString.contains("guai quan bu") ||
                pinyinString.contains("guai suo you") ||
                pinyinString.contains("da kuai quan bu") ||
                pinyinString.contains("da kuai suo you") ||
                pinyinString.contains("da kai suo you") ||
                pinyinString.contains("da kai quan bu") ||
                pinyinString.contains("da guai quan bu") ||
                pinyinString.contains("da guai suo you") ||
                pinyinString.contains("da gai quan bu") ||
                pinyinString.contains("da gai suo you") ||
                pinyinString.contains("xuan kuai") ||
                pinyinString.contains("xuan kai") ||
                pinyinString.contains("xuan guai") ||
                pinyinString.contains("xuan gai")) {//全开、开全部、开所有、打开全部、玄开


            if (mSelectDeviceCategory != null) {
                speackOK(R.raw.ok);
                allPower("00", "000100");
            } else {
                speackOK(R.raw.no_cmd);
            }

            return;

        } else if (pinyinString.contains("quan guan") ||
                pinyinString.contains("guan quan bu") ||
                pinyinString.contains("guan suo you") ||
                pinyinString.contains("guan bi suo you") ||
                pinyinString.contains("guan bi quan bu") ||
                pinyinString.contains("guan diao quan bu") ||
                pinyinString.contains("guan diao suo you") ||
                pinyinString.contains("quan gan") ||
                pinyinString.contains("gan quan bu") ||
                pinyinString.contains("gan suo you") ||
                pinyinString.contains("gan bi suo you") ||
                pinyinString.contains("gan bi quan bu") ||
                pinyinString.contains("gan diao quan bu") ||
                pinyinString.contains("gan diao suo you") ||
                pinyinString.contains("xuan guan")) {//全关、关全部、关所有、关闭全部、关闭所有、关掉全部、关掉所有、玄关

            if (mSelectDeviceCategory != null) {
                speackOK(R.raw.ok);
                allPower("00", "000000");
            } else {
                speackOK(R.raw.no_cmd);
            }

            return;

        }


        if (pinyinString.contains("kai deng") || pinyinString.contains("da kai deng") ||
                pinyinString.contains("guai deng") || pinyinString.contains("da guai deng")
                || pinyinString.contains("gai deng") || pinyinString.contains("da gai deng")
                || pinyinString.contains("tai deng") || pinyinString.contains("liang deng")) {//开灯、打开灯、台灯、亮灯

            speackOK(R.raw.ok);
            onGroup(content);
            return;

        } else if (pinyinString.contains("guan deng") ||
                pinyinString.contains("guan diao deng") ||
                pinyinString.contains("guan bi deng") ||
                pinyinString.contains("gan deng") ||
                pinyinString.contains("gan diao deng") ||
                pinyinString.contains("gan bi deng")) {//关灯、关掉灯、关闭灯
            speackOK(R.raw.ok);
            offGroup(content);
            return;

        }
        /* else if (pinyinString.contains("zhong xin")
                || pinyinString.contains("zong xin")
                || pinyinString.contains("zhong sheng")) {//中性光、中心光
            speackOK(R.raw.ok);
            controlNeutral();

            return;
        }*/

        else if (pinyinString.contains("se liang du") ||
                pinyinString.contains("shi liang dao") ||
                pinyinString.contains("shi liang dou") ||
                pinyinString.contains("se niang du") ||
                pinyinString.contains("shi niang dao") ||
                pinyinString.contains("shi niang dou") ||
                pinyinString.contains("se lang du") ||
                pinyinString.contains("shi lang dao") ||
                pinyinString.contains("shi lang dou")) {//色亮度
            controlColorLd(pinyinString);
            return;
        } else if (pinyinString.contains("se wen liang du") ||
                pinyinString.contains("se wen lang du") ||
                pinyinString.contains("se wen niang du")) {//色温亮度
            controlSwLd(pinyinString);
            return;
        } else if (pinyinString.contains("zui a") || pinyinString.contains("zui yan")
                || pinyinString.contains("zui xiao") || pinyinString.contains("ye guang")
                || pinyinString.contains("dui a")) {//最暗、最爱、最小、对岸
            controlLd2(1);

            return;
        } else if (pinyinString.contains("zui liang") ||
                pinyinString.contains("zui da") ||
                pinyinString.contains("zui niang") ||
                pinyinString.contains("zui lang") ||
                pinyinString.contains("dui liang")) {//最亮\最大、对亮
            controlLd2(2);
            return;
        } else if (pinyinString.contains("zui bai")
                || pinyinString.contains("zui pai")
                || pinyinString.contains("dui bai")
                ) {//最白
            controlWhite(2);

            return;
        } else if (pinyinString.contains("zui huang")
                || pinyinString.contains("zui wang")
                || pinyinString.contains("zui hang")
                || pinyinString.contains("dui huang")
                || pinyinString.contains("dui wang")) {//最黄
            controlYellow(2);

            return;

        } else if (pinyinString.contains("bai yi di") || pinyinString.contains("bai yi ｄ")
                || pinyinString.contains("bai yi die") || pinyinString.contains("bai dian")
                || pinyinString.contains("bian bai") || pinyinString.contains("zai bai")
                || pinyinString.contains("geng bai")
                || pinyinString.contains("pai yi di") || pinyinString.contains("pai yi ｄ")
                || pinyinString.contains("pai yi die") || pinyinString.contains("pai dian")
                || pinyinString.contains("bian pai") || pinyinString.contains("zai pai")
                || pinyinString.contains("geng pai")
                || pinyinString.contains("leng yi di") || pinyinString.contains("leng yi ｄ")
                || pinyinString.contains("leng yi die") || pinyinString.contains("leng dian")
                || pinyinString.contains("bian leng") || pinyinString.contains("zai leng")
                || pinyinString.contains("geng leng")
                ) {//白一点、白点、变白、再白、更白、冷一点、冷点、变冷、再冷、更冷
            controlSw(pinyinString);
            return;


        } else if (pinyinString.contains("huang yi di") || pinyinString.contains("huang dian")
                || pinyinString.contains("zai huang") || pinyinString.contains("bian huang")
                || pinyinString.contains("geng huang") || pinyinString.contains("huang yi ｄ")
                || pinyinString.contains("nuan yi di") || pinyinString.contains("nuan dian")
                || pinyinString.contains("zai nuan") || pinyinString.contains("bian nuan")
                || pinyinString.contains("geng nuan") || pinyinString.contains("nuan yi ｄ")
                || pinyinString.contains("wang yi di") || pinyinString.contains("wang dian")
                || pinyinString.contains("zai wang") || pinyinString.contains("bian wang")
                || pinyinString.contains("geng wang") || pinyinString.contains("wang yi ｄ")
                || pinyinString.contains("hang yi di") || pinyinString.contains("hang dian")
                || pinyinString.contains("zai hang") || pinyinString.contains("bian hang")
                || pinyinString.contains("geng hang") || pinyinString.contains("hang yi ｄ")
                ) {//黄一点、黄点、变黄、再黄、更黄、暖一点、暖点、变暖、再暖、更暖
            controlSw(pinyinString);
            return;
        } else if (isControlLd(pinyinString)) {//根据当前模式调整亮度

            controlLd(pinyinString, 0);
            return;

        } else if (pinyinString.contains("se wen") || pinyinString.contains("she wen")) {//色温
            controlSw(pinyinString);
            return;
        }

        //红，橙，黄，绿，青 蓝，紫，白
        else if (pinyinString.contains("fen hong se") || pinyinString.contains("fen hong she")) {//粉红色
            changeColor(content, 10);
            return;
        } else if (pinyinString.contains("cheng se") || pinyinString.contains("cheng she") ||
                pinyinString.contains("ceng se") || pinyinString.contains("ceng she")) {//橙色
            changeColor(content, 2);
            return;
        } else if (pinyinString.contains("hong se") || pinyinString.contains("hong she") ||
                pinyinString.contains("heng se") || pinyinString.contains("heng she")) {//红色
            changeColor(content, 1);
            return;
        } else if (pinyinString.contains("huang se") || pinyinString.contains("wang se")
                || pinyinString.contains("hang se") || pinyinString.contains("huang she")
                || pinyinString.contains("wang she") || pinyinString.contains("hang she")) {//黄色
            changeColor(content, 3);
            return;
        } else if (pinyinString.contains("lv se") || pinyinString.contains("lv she") ||
                pinyinString.contains("li se") || pinyinString.contains("li she") ||
                pinyinString.contains("nv se") || pinyinString.contains("nv she")) {//绿色
            changeColor(content, 4);
            return;
        } else if (pinyinString.contains("qing se") || pinyinString.contains("qing she")
                || pinyinString.contains("qing xi")) {//青色
            changeColor(content, 5);
            return;
        } else if (pinyinString.contains("lan se") || pinyinString.contains("lan she") ||
                pinyinString.contains("nan se") || pinyinString.contains("nan she")) {//蓝色
            changeColor(content, 6);
            return;
        } else if (pinyinString.contains("zi se") || pinyinString.contains("zi shi")
                || pinyinString.contains("zhi se") || pinyinString.contains("zi she")
                || pinyinString.contains("zhi she")) {//紫色
            changeColor(content, 7);
            return;
        } else if (pinyinString.contains("bai se") || pinyinString.contains("bai she") ||
                pinyinString.contains("pai se") || pinyinString.contains("pai she")) {//白色
            changeColor(content, 8);
            return;
        } else if (pinyinString.contains("fen se") || pinyinString.contains("fen she")) {//粉色
            changeColor(content, 9);
            return;
        } else if (pinyinString.contains("ri chu") || pinyinString.contains("ri cu")) {//日出
            speackOK(R.raw.ok);
            selectGroup.setRgb("ffffff");
            selectGroup.setMode(1);
            controlScene(selectGroup, "ffffff41", "0affff");

            return;
        } else if (pinyinString.contains("ri luo") || pinyinString.contains("ri lao")) {//日落
            speackOK(R.raw.ok);
            selectGroup.setRgb("ffffff");
            selectGroup.setMode(1);
            controlScene(selectGroup, "ffffff41", "ff0aff");

            return;
        } else if (pinyinString.contains("hu xi deng") || pinyinString.contains("hu si deng")) {//呼吸灯
            String data1 = selectGroup.getRgb() + "81";
            String data2 = "02FF02";
            speackOK(R.raw.ok);
            selectGroup.setMode(1);
            controlScene(selectGroup, data1, data2);
            return;
        } else if (pinyinString.contains("qi cai jian bian")) {//七彩渐变
            speackOK(R.raw.ok);
            selectGroup.setMode(1);
            controlScene(selectGroup, "ffffff51", "ffffff");
            return;
        } else if (pinyinString.contains("qi cai shan shuo") ||
                pinyinString.contains("qi cai san shuo")) {//七彩闪烁
            speackOK(R.raw.ok);
            selectGroup.setMode(1);
            controlScene(selectGroup, "ffffff31", "ff0aff");
            return;
        } else if (pinyinString.contains("yue du") || pinyinString.contains("ye du")) {//阅读
            speackOK(R.raw.ok);
            selectGroup.setRgb("ffffff");
            selectGroup.setLd1(118);
            selectGroup.setMode(1);
            controlScene(selectGroup, "ffffff01", "7f7fff");

            return;
        } else if (pinyinString.contains("hai bian")
                || pinyinString.contains("hai pian")
                || pinyinString.contains("hai ban")) {//海边

            speackOK(R.raw.ok);
            selectGroup.setRgb("007fff");
            selectGroup.setLd1(235);
            selectGroup.setMode(1);
            controlScene(selectGroup, "007fff01", "ffffff");

            return;
        } else if (pinyinString.contains("zhu guang") || pinyinString.contains("zhu gang") ||
                pinyinString.contains("zu gang") || pinyinString.contains("zu gang") ||
                pinyinString.contains("chu gang") || pinyinString.contains("chu gang")) {//烛光
            speackOK(R.raw.ok);
            selectGroup.setRgb("ff9905");
            selectGroup.setLd1(235);
            selectGroup.setMode(1);
            controlScene(selectGroup, "ff990501", "ffffff");

            return;
        } else if (pinyinString.contains("fen duan") || pinyinString.contains("fen dan") || pinyinString.contains("bian guang") || pinyinString.contains("xia yi d") || pinyinString.contains("xia yi z")
                || pinyinString.contains("yan guang") || pinyinString.contains("dian guang")) {//温馨改为分段
            selectGroup.setMode(2);
            mBallLightDao.updateBallLight(selectGroup);
            speackOK(R.raw.ok);
            if (selectGroup.getFamilyId().equals("12345678")) {
                mDefaultBallLight = selectGroup;
            } else if (!selectGroup.getFamilyId().equals("12345678") && selectGroup.getGroupId().equals("00")) {
                mAllGroup = selectGroup;
            }

            commandCmd("08", selectGroup.getFamilyId(), selectGroup.getGroupId(), "00000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback());
            return;
        } else if (pinyinString.contains("huang gua") || pinyinString.contains("nuan gua") || pinyinString.contains("nuan se")) {//阳光修改为黄光、暖光、暖色
            selectGroup.setSw(255);
            selectGroup.setLd1(235);
            selectGroup.setMode(2);
            speackOK(R.raw.ok);
            controlScene(selectGroup, "ff000000", "ffffff");
            return;
        } else if (pinyinString.contains("zhong xin")
                || pinyinString.contains("zong xin")
                || pinyinString.contains("zhong sheng")) {//饮食修改为中性光
            selectGroup.setSw(127);
            selectGroup.setLd1(235);
            selectGroup.setMode(2);
            speackOK(R.raw.ok);
            controlScene(selectGroup, "7f7f0000", "ffffff");

            return;
        } else if (pinyinString.contains("xia wu")
                || pinyinString.contains("ha wu")
                || pinyinString.contains("rou he")
                || pinyinString.contains("liu he")
                || pinyinString.contains("you he")
                || pinyinString.contains("ru he")) {//下午、柔和

            selectGroup.setSw(127);
            selectGroup.setLd1(118);
            selectGroup.setMode(2);
            speackOK(R.raw.ok);
            controlScene(selectGroup, "7f7f0000", "7f7fff");

            return;
        } else if (pinyinString.contains("yue guang") || pinyinString.contains("yue gang") ||
                pinyinString.contains("ye guang") || pinyinString.contains("ye gang")) {//月光

            selectGroup.setSw(0);
            selectGroup.setLd1(118);
            selectGroup.setMode(2);
            speackOK(R.raw.ok);
            controlScene(selectGroup, "00ff0000", "7f7fff");

            return;
        } else if (pinyinString.contains("bai guang") || pinyinString.contains("bai gang") || pinyinString.contains("leng gua")) {//纯白改成白光、冷光

            selectGroup.setSw(0);
            selectGroup.setLd1(235);
            selectGroup.setMode(2);
            speackOK(R.raw.ok);
            controlScene(selectGroup, "00ff0000", "ffffff");

            return;
        } else if (pinyinString.contains("xiao ye deng") || pinyinString.contains("ye guang")) {//小夜灯、夜光
            selectGroup.setSw(127);
            selectGroup.setLd1(0);
            selectGroup.setMode(2);
            speackOK(R.raw.ok);
            controlScene(selectGroup, "7f7f0000", "1414ff");

            return;
        } else if (pinyinString.contains("fu zhu deng") || pinyinString.contains("fu deng")
                || pinyinString.contains("fo deng") || pinyinString.contains("pu deng")
                || pinyinString.contains("pu dong")) {//辅助灯、辅灯
            boolean isOn = SPUtil.getBool(Constants.ISON, false);
            if (pinyinString.contains("kai")) {
                commandSceneCmd("01", selectGroup.getFamilyId(), selectGroup.getGroupId(), "00000000", "000100", mCurrFamily.getSeed(), mCurrFamily.getFeedback());
                speackOK(R.raw.ok);
                isOn = true;
                SPUtil.put(Constants.ISON, isOn);
            } else if (pinyinString.contains("guan") || pinyinString.contains("gan")) {
                commandSceneCmd("01", selectGroup.getFamilyId(), selectGroup.getGroupId(), "00000000", "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback());
                speackOK(R.raw.ok);
                isOn = false;
                SPUtil.put(Constants.ISON, isOn);
            } else {
                speackOK(R.raw.no_cmd);
            }

            return;
        } else if (pinyinString.contains("kai") || pinyinString.contains("guai")) {//开
            speackOK(R.raw.ok);
            onGroup(content);
            return;

        } else if ((pinyinString.contains("guan") || pinyinString.contains("hei") || pinyinString.contains("mie") || pinyinString.contains("gan") || pinyinString.contains("bi")) /*&& content.length() > 2*/) {//关、黑
            speackOK(R.raw.ok);

            onOrOff(selectGroup.getGroupId(), "000000");
            return;
        } else if ((pinyinString.contains("liang") || pinyinString.contains("ming"))) {//亮、明
            controlLd("liang yi di", 0);//调用亮一点命令

            return;
        } else if ((pinyinString.contains("huang") || pinyinString.contains("nuan"))) {//黃、暖
            controlSw("huang yi di");//调用黃一点命令

            return;
        } else if ((pinyinString.contains("hun") || pinyinString.contains("an"))) {//昏、暗
            controlLd("an yi di", 0);//调用暗一点的命令

            return;
        } else if ((pinyinString.contains("bai") || pinyinString.contains("leng"))) {//白、冷
            controlSw("bai yi di");//调用白一点命令

            return;
        } else if (checkGroupAnother(pinyinString)) {
            speackOK(R.raw.ok);
            return;
        } else {
            speackOK(R.raw.no_cmd);
        }
    }


    private void checkGroup(String pinyinString) {//bug:例如组名里面有我家风扇和我家风扇灯，他会优先识别到我家风扇，然后跳出循环。
        if (mCurrFamily == null) return;
        List<String> groupNameLists = new ArrayList<>();
        String familyID = mCurrFamily.getFamilyId();
        List<DeviceCategory> allLights = mDeviceCategoryDao.getFamilyDeviceCategorys(familyID);

        if (!mCurrFamily.isHideBallLight()) {
            allLights.add(mDefaultDeviceCategoryLight);//添加默认灯
        }


        if (!mCurrFamily.isHideFanLamp()) {
            allLights.add(mDefaultDeviceCategoryFanLamp);//添加默认风扇灯}
        }

        if (!mCurrFamily.isHideDataFanLamp()) {
            allLights.add(mDefaultDeviceCategoryDataFanLamp);//添加默认发送数据的风扇灯}
        }


        if (!mCurrFamily.isHideFan()) {
            allLights.add(mDefaultDeviceCategoryFan);//添加默认风扇}
        }


        if (!mCurrFamily.isHideHumidifier()) {
            allLights.add(mDefaultDeviceCategoryHumidifier);//添加默认加湿器}
        }

        if (!mCurrFamily.isHideBathHeater()) {
            allLights.add(mDefaultDeviceCategoryBathHeater);//添加默认浴霸
        }


        for (DeviceCategory light : allLights) {
            groupNameLists.add(light.getName());
        }

        List<String> sortStringList = sortStringList(groupNameLists);

        String name = findMatchName(pinyinString, sortStringList);
        if (!TextUtils.isEmpty(name)) {
            if (name.equals(sContext.getString(R.string.default_device))) {
                mSelectDeviceCategory = mDefaultDeviceCategoryLight;
            } else if (name.equals(sContext.getString(R.string.default_fanlamp1))) {
                mSelectDeviceCategory = mDefaultDeviceCategoryFanLamp;
            } else if (name.equals(sContext.getString(R.string.default_fanlamp2))) {
                mSelectDeviceCategory = mDefaultDeviceCategoryDataFanLamp;
            } else if (name.equals(sContext.getString(R.string.default_fan))) {
                mSelectDeviceCategory = mDefaultDeviceCategoryFan;
            } else if (name.equals(sContext.getString(R.string.default_Humidifier))) {
                mSelectDeviceCategory = mDefaultDeviceCategoryHumidifier;
            } else if (name.equals(sContext.getString(R.string.default_BathHeater))) {
                mSelectDeviceCategory = mDefaultDeviceCategoryBathHeater;
            } else {
                mSelectDeviceCategory = mDeviceCategoryDao.getDeviceCategoryByName(mCurrFamily.getFamilyId(), name);
            }
            if (mSelectDeviceCategory != null) {
                if (mSelectDeviceCategory.getFamilyId().equalsIgnoreCase("12345678")) {
                    selectGroup = mDefaultBallLight;
                } else {
                    selectGroup = mBallLightDao.getByUuid(mSelectDeviceCategory.getUuid());
                }


                mSelectDeviceCategory.setChecked(true);
                EventBus.getDefault().post(new MyEvent(Constants.SELECTGROUP, mSelectDeviceCategory));
                if (pinyinString.contains("xuan ze") || pinyinString.contains("xuan zhong")) {//选择、选中
                    speackOK(R.raw.ok);
                    return;
                }
            }
        }
    }


    private String findMatchName(String pinyinString, List<String> sortStringList) {
        try {
            for (String name : sortStringList) {
                String pyName = getChinese(name);
                pyName = PinyinHelper.convertToPinyinString(pyName, " ", PinyinFormat.WITHOUT_TONE);
                if (pinyinString.contains(pyName)) {
                    return name;
                }
            }
        } catch (PinyinException e) {
            e.printStackTrace();
        }
        return "";
    }

    public List<String> sortStringList(List<String> list) {
        String[] array = (String[]) list.toArray(new String[list.size()]);
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if (array[i].length() < array[j].length()) {
                    String temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
            }
        }
        return Arrays.asList(array);
    }


    public static String getChinese(String str) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            switch (c) {
                case '0':
                    sb.append("零");
                    break;
                case '1':
                    sb.append("一");
                    break;
                case '2':
                    sb.append("二");
                    break;
                case '3':
                    sb.append("三");
                    break;
                case '4':
                    sb.append("四");
                    break;
                case '5':
                    sb.append("五");
                    break;
                case '6':
                    sb.append("六");
                    break;
                case '7':
                    sb.append("七");
                    break;
                case '8':
                    sb.append("八");
                    break;
                case '9':
                    sb.append("九");
                    break;
                default:
                    sb.append(c);
            }
        }
        return sb.toString();
    }


    private boolean checkGroupAnother(String pinyinString) {

        if (mCurrFamily == null) return false;
        List<String> groupNameLists = new ArrayList<>();
        String familyID = mCurrFamily.getFamilyId();
        List<DeviceCategory> allLights = mDeviceCategoryDao.getFamilyDeviceCategorys(familyID);
        if (!mCurrFamily.isHideBallLight()) {
            allLights.add(mDefaultDeviceCategoryLight);//添加默认灯
        }


        if (!mCurrFamily.isHideFanLamp()) {
            allLights.add(mDefaultDeviceCategoryFanLamp);//添加默认风扇灯}
        }


        if (!mCurrFamily.isHideDataFanLamp()) {
            allLights.add(mDefaultDeviceCategoryDataFanLamp);//添加默认数据风扇灯}
        }


        if (!mCurrFamily.isHideFan()) {
            allLights.add(mDefaultDeviceCategoryFan);//添加默认风扇}
        }


        if (!mCurrFamily.isHideHumidifier()) {
            allLights.add(mDefaultDeviceCategoryHumidifier);//添加默认加湿器}
        }


        if (!mCurrFamily.isHideBathHeater()) {
            allLights.add(mDefaultDeviceCategoryBathHeater);//添加默认浴霸}
        }


        for (DeviceCategory light : allLights) {
            groupNameLists.add(light.getName());
        }

        List<String> sortStringList = sortStringList(groupNameLists);

        String name = findMatchName(pinyinString, sortStringList);


        if (!TextUtils.isEmpty(name)) {
            if (name.equals(sContext.getString(R.string.default_device))) {
                mSelectDeviceCategory = mDefaultDeviceCategoryLight;
            } else if (name.equals(sContext.getString(R.string.default_fanlamp1))) {
                mSelectDeviceCategory = mDefaultDeviceCategoryFanLamp;
            } else if (name.equals(sContext.getString(R.string.default_fanlamp2))) {
                mSelectDeviceCategory = mDefaultDeviceCategoryDataFanLamp;
            } else if (name.equals(sContext.getString(R.string.default_fan))) {
                mSelectDeviceCategory = mDefaultDeviceCategoryFan;
            } else if (name.equals(sContext.getString(R.string.default_Humidifier))) {
                mSelectDeviceCategory = mDefaultDeviceCategoryHumidifier;
            } else if (name.equals(sContext.getString(R.string.default_BathHeater))) {
                mSelectDeviceCategory = mDefaultDeviceCategoryBathHeater;
            } else {
                mSelectDeviceCategory = mDeviceCategoryDao.getDeviceCategoryByName(mCurrFamily.getFamilyId(), name);
            }
            if (mSelectDeviceCategory != null) {
                if (mSelectDeviceCategory.getFamilyId().equalsIgnoreCase("12345678")) {
                    selectGroup = mDefaultBallLight;
                } else {
                    selectGroup = mBallLightDao.getByUuid(mSelectDeviceCategory.getUuid());
                }
                return true;
            }

        }

        return false;
    }


//    private void speackOK(int voice) {
//        VoiceHelper.getInstance(sContext).initMediaPlay(voice, new VoiceHelper.VoicePlayCallBack() {
//            @Override
//            public void playFinish() {
//
//            }
//
//            @Override
//            public void playError() {
//
//            }
//        });
//    }

    public void speackOK(int resId) {
        EventBus.getDefault().post(new MyEvent(Constants.SPEECH_CMD, resId));
//
    }

//    //通知启动唤醒(1)还是识别(0)
//    public void notifyBDSpeech(int type){
//        EventBus.getDefault().post(new MyEvent(Constants.SPEECH_CMD,type));
//
//    }

    /**
     * 中文
     * 控制颜色亮度
     *
     * @param content
     */
    private void controlColorLd(String content) {
        BallLight group = selectGroup;
        if (group == null) {
            speackOK(R.raw.select_group);
            myToast(sContext.getString(R.string.select_group_first), true);
            return;
        }
        int ld = group.getLd1();
        if (content.contains("liang du jian") || content.contains("liang du di")
                || content.contains("liang du gan") || content.contains("liang du qiang")
                || content.contains("niang du jian") || content.contains("niang du di")
                || content.contains("niang du gan")
                || content.contains("lang du jian") || content.contains("lang du di")
                || content.contains("lang du gan")
                || content.contains("an yi ｄ")
                || content.contains("an yi di") || content.contains("yan yi di")
                || content.contains("yan １ dian") || content.contains("ying yi dian")
                || content.contains("an yi dian") || content.contains("an dian")
                || content.contains("zai an") || content.contains("geng an")
                || content.contains("bian an") || content.contains("hun dian")
                || content.contains("hun yi dian") || content.contains("zai hun")
                || content.contains("geng hun") || content.contains("bian hun")
                || content.contains("liang du bian di")) {//减小、减少、暗一点、暗点、再暗、更暗、变暗、昏点、昏一点、再昏、更昏、变昏
            ld -= 50;
            ld = ld < 0 ? 0 : ld;
            group.setLd1(ld);
            group.setMode(1);

            speackOK(R.raw.ok);

//            controlRGB(group, "80");
            controlRGB(group, "00");
            mBallLightDao.updateBallLight(group);
            selectGroup = group;

            if (selectGroup.getFamilyId().equals("12345678")) {
                mDefaultBallLight = group;
            } else if (!selectGroup.getFamilyId().equals("12345678") && selectGroup.getGroupId().equals("00")) {
                mAllGroup = selectGroup;
            }


        } else if (content.contains("liang du zeng") || content.contains("liang du gao")
                || content.contains("liang du jia") || content.contains("liang du shen")
                || content.contains("liang yi ｄ") || content.contains("liang yi di")
                || content.contains("niang du zeng") || content.contains("niang du gao")
                || content.contains("niang du jia") || content.contains("niang du shen")
                || content.contains("niang yi ｄ") || content.contains("niang yi di")
                || content.contains("lang du zeng") || content.contains("lang du gao")
                || content.contains("lang du jia") || content.contains("lang du shen")
                || content.contains("lang yi ｄ") || content.contains("liang yi di")
                || content.contains("ming yi dian")
                || content.contains("liang dian") || content.contains("zai liang")
                || content.contains("geng liang") || content.contains("bian liang")
                || content.contains("niang dian") || content.contains("zai niang")
                || content.contains("geng niang") || content.contains("bian niang")
                || content.contains("lang dian") || content.contains("zai lang")
                || content.contains("geng lang") || content.contains("bian lang")
                ) {//增加、增大、亮一点、明一点、亮点、再亮、更亮、变亮
            ld += 50;
            ld = ld > 235 ? 235 : ld;

            group.setLd1(ld);
            group.setMode(1);

            speackOK(R.raw.ok);

//            controlRGB(group, "80");
            controlRGB(group, "00");
            mBallLightDao.updateBallLight(group);
            selectGroup = group;
            if (selectGroup.getFamilyId().equals("12345678")) {
                mDefaultBallLight = group;
            } else if (!selectGroup.getFamilyId().equals("12345678") && selectGroup.getGroupId().equals("00")) {
                mAllGroup = selectGroup;
            }
        } else {
            speackOK(R.raw.no_cmd);
        }

    }

    /**
     * 中文
     * 控制色温亮度(黄白)
     *
     * @param content
     */
    private void controlSwLd(String content) {
        BallLight group = selectGroup;
        if (group == null) {
            speackOK(R.raw.select_group);
            myToast(sContext.getString(R.string.select_group_first), true);
            return;
        }

        int ld = group.getLd1();

        if (content.contains("liang du jian") || content.contains("liang du di")
                || content.contains("liang du gan") || content.contains("liang du qiang")
                || content.contains("niang du jian") || content.contains("niang du di")
                || content.contains("niang du gan")
                || content.contains("lang du jian") || content.contains("lang du di")
                || content.contains("lang du gan")
                || content.contains("an yi ｄ")
                || content.contains("an yi di") || content.contains("yan yi di")
                || content.contains("yan １ dian") || content.contains("ying yi dian")
                || content.contains("an yi dian") || content.contains("an dian")
                || content.contains("zai an") || content.contains("geng an")
                || content.contains("bian an") || content.contains("hun dian")
                || content.contains("hun yi dian") || content.contains("zai hun")
                || content.contains("geng hun") || content.contains("bian hun")
                || content.contains("liang du bian di")) {//减小、减少、暗一点、暗点、再暗、更暗、变暗、昏点、昏一点、再昏、更昏、变昏
            ld -= 50;
            ld = ld < 0 ? 0 : ld;
            speackOK(R.raw.ok);

            group.setLd1(ld);
            group.setMode(2);

            controlSW(group, "00");

            mBallLightDao.updateBallLight(group);
            selectGroup = group;
            if (selectGroup.getFamilyId().equals("12345678")) {
                mDefaultBallLight = group;
            } else if (!selectGroup.getFamilyId().equals("12345678") && selectGroup.getGroupId().equals("00")) {
                mAllGroup = selectGroup;
            }
        } else if (content.contains("liang du zeng") || content.contains("liang du gao")
                || content.contains("liang du jia") || content.contains("liang du shen")
                || content.contains("liang yi ｄ") || content.contains("liang yi di")
                || content.contains("niang du zeng") || content.contains("niang du gao")
                || content.contains("niang du jia") || content.contains("niang du shen")
                || content.contains("niang yi ｄ") || content.contains("niang yi di")
                || content.contains("lang du zeng") || content.contains("lang du gao")
                || content.contains("lang du jia") || content.contains("lang du shen")
                || content.contains("lang yi ｄ") || content.contains("liang yi di")
                || content.contains("ming yi dian")
                || content.contains("liang dian") || content.contains("zai liang")
                || content.contains("geng liang") || content.contains("bian liang")
                || content.contains("niang dian") || content.contains("zai niang")
                || content.contains("geng niang") || content.contains("bian niang")
                || content.contains("lang dian") || content.contains("zai lang")
                || content.contains("geng lang") || content.contains("bian lang")) {//增加、增大、亮一点、明一点、亮点、再亮、更亮、变亮
            ld += 50;
            ld = ld > 235 ? 235 : ld;
            speackOK(R.raw.ok);

            group.setLd1(ld);
            group.setMode(2);

            controlSW(group, "00");

            mBallLightDao.updateBallLight(group);
            selectGroup = group;
            if (selectGroup.getFamilyId().equals("12345678")) {
                mDefaultBallLight = group;
            } else if (!selectGroup.getFamilyId().equals("12345678") && selectGroup.getGroupId().equals("00")) {
                mAllGroup = selectGroup;
            }
        } else {
            speackOK(R.raw.no_cmd);
        }


    }

    /**
     * 中文
     * 控制色温变化
     *
     * @param content
     */
    private void controlSw(String content) {
        BallLight group = selectGroup;
        if (group == null) {
            speackOK(R.raw.select_group);
            myToast(sContext.getString(R.string.select_group_first), true);
            return;
        }
        int sw = group.getSw();

        if (content.contains("se wen jian") || content.contains("she wen jian") ||
                content.contains("se wen di") || content.contains("she wen di") ||
                content.contains("gan xiao") || content.contains("gan shao") ||
                content.contains("xian fei") || content.contains("xian xi") ||
                content.contains("huang yi dian") || content.contains("huang yi di") ||
                content.contains("huang dian") || content.contains("zai huang") ||
                content.contains("bian huang") || content.contains("geng huang") ||
                content.contains("nuan yi dian") || content.contains("nuan dian") ||
                content.contains("zai nuan") || content.contains("bian nuan") ||
                content.contains("geng nuan") || content.contains("huang yi ｄ") ||
                content.contains("wang yi dian") || content.contains("wang yi di") ||
                content.contains("wang yi ｄ") || content.contains("hang yi dian") ||
                content.contains("hang yi di") || content.contains("hang yi ｄ") ||
                content.contains("se wen bian di") || content.contains("se wen de") ||
                content.contains("se wen qiang") || content.contains("she wen de")
                ) {//减小、减少、色温降、色温减、色温低、黄一点、黄点、再黄、变黄、更黄、暖一点、暖点、再暖、变暖、更暖
            sw += 50;
//            sw = sw < 127 ? (127 + 10) : sw;
            sw = sw > 255 ? 255 : sw;

            speackOK(R.raw.ok);

            group.setSw(sw);
            group.setMode(2);
            mBallLightDao.updateBallLight(group);
            selectGroup = group;
            if (selectGroup.getFamilyId().equals("12345678")) {
                mDefaultBallLight = group;
            } else if (!selectGroup.getFamilyId().equals("12345678") && selectGroup.getGroupId().equals("00")) {
                mAllGroup = selectGroup;
            }
            controlSW(group, "00");


        } else if (content.contains("se wen zeng") || content.contains("se wen gao")
                || content.contains("se wen jia") || content.contains("se wen shen")
                || content.contains("she wen zeng") || content.contains("she wen gao")
                || content.contains("she wen jia") || content.contains("she wen shen")
                || content.contains("ce wen zeng") || content.contains("ce wen gao")
                || content.contains("ce wen jia") || content.contains("ce wen shen")
                || content.contains("se wen zhen") || content.contains("she wen zhen")
                || content.contains("ce wen zhen") || content.contains("ce wen zheng")
                || content.contains("se wen zheng") || content.contains("she wen zheng")
                || content.contains("bai yi di") || content.contains("bai yi ｄ")
                || content.contains("bai yi die") || content.contains("bai dian")
                || content.contains("bian bai") || content.contains("zai bai")
                || content.contains("geng bai")
                || content.contains("pai yi di") || content.contains("pai yi ｄ")
                || content.contains("pai yi die") || content.contains("pai dian")
                || content.contains("bian pai") || content.contains("zai pai")
                || content.contains("geng pai")
                || content.contains("leng yi di") || content.contains("leng yi ｄ")
                || content.contains("leng yi die") || content.contains("leng dian")
                || content.contains("bian leng") || content.contains("zai leng")
                || content.contains("geng leng")
                || content.contains("se wen bian gao")
                ) {//增加、增大、色温增、色温高、色温加、色温升、白一点、白点、变白、再白、更白、冷一点、冷点、变冷、再冷、更冷
            sw -= 50;
//            sw = sw < 127 ? (127 + 10) : sw;
            sw = sw < 0 ? 0 : sw;

            speackOK(R.raw.ok);

            group.setSw(sw);
            group.setMode(2);
            mBallLightDao.updateBallLight(group);
            selectGroup = group;
            if (selectGroup.getFamilyId().equals("12345678")) {
                mDefaultBallLight = group;
            } else if (!selectGroup.getFamilyId().equals("12345678") && selectGroup.getGroupId().equals("00")) {
                mAllGroup = selectGroup;
            }
            controlSW(group, "00");
        } else {
            speackOK(R.raw.no_cmd);
        }

    }

    private boolean isControlLd(String content) {
        if (content.contains("liang du zeng") || content.contains("liang du gao")
                || content.contains("liang du jia") || content.contains("liang du shen")
                || content.contains("liang yi ｄ") || content.contains("liang yi di")
                || content.contains("niang du zeng") || content.contains("niang du gao")
                || content.contains("niang du jia") || content.contains("niang du shen")
                || content.contains("niang yi ｄ") || content.contains("niang yi di")
                || content.contains("lang du zeng") || content.contains("lang du gao")
                || content.contains("lang du jia") || content.contains("lang du shen")
                || content.contains("lang yi ｄ") || content.contains("liang yi di")
                || content.contains("ming yi dian")
                || content.contains("liang dian") || content.contains("zai liang")
                || content.contains("geng liang") || content.contains("bian liang")
                || content.contains("niang dian") || content.contains("zai niang")
                || content.contains("geng niang") || content.contains("bian niang")
                || content.contains("lang dian") || content.contains("zai lang")
                || content.contains("geng lang") || content.contains("bian lang")
                || content.contains("liang du jian") || content.contains("liang du di")
                || content.contains("liang du gan") || content.contains("liang du qiang")
                || content.contains("niang du jian") || content.contains("niang du di")
                || content.contains("niang du gan")
                || content.contains("lang du jian") || content.contains("lang du di")
                || content.contains("lang du gan")
                || content.contains("an yi ｄ")
                || content.contains("an yi di") || content.contains("yan yi di")
                || content.contains("yan １ dian") || content.contains("ying yi dian")
                || content.contains("an yi dian") || content.contains("an dian")
                || content.contains("zai an") || content.contains("geng an")
                || content.contains("bian an") || content.contains("hun dian")
                || content.contains("hun yi dian") || content.contains("zai hun")
                || content.contains("geng hun") || content.contains("bian hun")
                || content.contains("liang du bian di")) {//亮度增大、亮度增加、亮一点、亮度减小、亮度减少、暗一点
            return true;
        }
        return false;


    }


    private void controlWhite(int type) {
        BallLight group = selectGroup;
        if (group == null) {
            speackOK(R.raw.select_group);
            myToast(sContext.getString(R.string.select_group_first), true);
            return;
        }

        int sw = 0;
        if (type == 2) {
            sw = 0;
        } else {
            sw = group.getSw() - 10;
            sw = sw > 127 ? (127 - 10) : sw;
            sw = sw < 0 ? 0 : sw;
        }
        speackOK(R.raw.ok);
        group.setSw(sw);
        group.setMode(2);
        controlSW(group, "00");
        mBallLightDao.updateBallLight(group);
        selectGroup = group;
        if (selectGroup.getFamilyId().equals("12345678")) {
            mDefaultBallLight = group;
        } else if (!selectGroup.getFamilyId().equals("12345678") && selectGroup.getGroupId().equals("00")) {
            mAllGroup = selectGroup;
        }

    }

    public void controlYellow(int type) {
        BallLight group = selectGroup;
        if (group == null) {
            speackOK(R.raw.select_group);
            myToast(sContext.getString(R.string.select_group_first), true);
            return;
        }
        int sw;
        if (type == 2) sw = 255;
        else {
            sw = group.getSw() + 10;
            sw = sw < 127 ? (127 + 10) : sw;
            sw = sw > 255 ? 255 : sw;
        }
        speackOK(R.raw.ok);
        group.setSw(sw);
        group.setMode(2);
        controlSW(group, "00");
        mBallLightDao.updateBallLight(group);
        selectGroup = group;
        if (selectGroup.getFamilyId().equals("12345678")) {
            mDefaultBallLight = group;
        } else if (!selectGroup.getFamilyId().equals("12345678") && selectGroup.getGroupId().equals("00")) {
            mAllGroup = selectGroup;
        }

    }


    public void setSelectGroupListener(SelectGroupListener selectGroupListener) {
        mSelectGroupListener = selectGroupListener;
    }


    // //红，橙，黄，绿，青 蓝，紫，白
    private void changeColor(String content, int type) {
        BallLight group = selectGroup;
        if (group == null) {
            speackOK(R.raw.select_group);
            myToast(sContext.getString(R.string.select_group_first), true);
            return;
        }
        String rgbHex = getPwmHexs(255, 255, 255);
        group.setMode(1);
        switch (type) {
            case 1://红
                rgbHex = getPwmHexs(255, 0, 0);
                break;
            case 2://橙
                rgbHex = getPwmHexs(255, 127, 0);
                break;
            case 3://黄;
                rgbHex = getPwmHexs(255, 255, 0);
                break;
            case 4://绿
                rgbHex = getPwmHexs(0, 255, 0);
                break;
            case 5://青
                rgbHex = getPwmHexs(0, 255, 255);
                break;
            case 6://蓝
                rgbHex = getPwmHexs(0, 0, 255);
                break;
            case 7://紫
                rgbHex = getPwmHexs(255, 0, 255);
                break;
            case 8://白
                rgbHex = getPwmHexs(255, 255, 255);
                break;
            case 9://粉色
                rgbHex = getPwmHexs(232, 147, 213);
                break;
            case 10://粉红色
                rgbHex = getPwmHexs(216, 75, 184);
                break;
        }
        group.setRgb(rgbHex);
        speackOK(R.raw.ok);
        LogHelper.i("change color type=" + type);
        controlRGB(group, "00");
        selectGroup = group;
        if (selectGroup.getFamilyId().equals("12345678")) {
            mDefaultBallLight = group;
        } else if (!selectGroup.getFamilyId().equals("12345678") && selectGroup.getGroupId().equals("00")) {
            mAllGroup = selectGroup;
        }
    }

    private String getPwmHexs(int red, int green, int blue) {
        String redHex = BleDataHelper.numberToHex(red);
        String greenHex = BleDataHelper.numberToHex(green);
        String blueHex = BleDataHelper.numberToHex(blue);
        String pwmHexs = new StringBuilder().append(redHex).append(greenHex).append(blueHex).toString();
        return pwmHexs;
    }


//    //红 、橙，黄， 绿，青，蓝，紫
//    public String[] colors = {"FF0000", "FF7F00", "FFFF00",
//            "00FF00", "00FFFF", "0000FF", "8B00FF"};

    private void offGroup(String content) {
        BallLight group = selectGroup;
        if (group == null) {
            speackOK(R.raw.select_group);
            myToast(sContext.getString(R.string.select_group_first), true);
            return;
        }
        onOrOff(group.getGroupId(), "000000");

    }


    private void onGroup(String content) {
        BallLight group = selectGroup;
        if (group == null) {
            speackOK(R.raw.select_group);
            myToast(sContext.getString(R.string.select_group_first), true);
            return;
        }
        onOrOff(group.getGroupId(), "000100");

    }


    public String numToHex(int number) {
        String hex = Integer.toHexString(number).toUpperCase();
        hex = hex.length() % 2 == 0 ? hex : "0" + hex;
        return hex;
    }

    public String getByteHex(int b1) {
        String hex = Integer.toString(b1, 16).toUpperCase();
        if (hex.length() < 2) hex = "0" + hex;
        return hex;
    }


    protected int strToInt(String str) {
        try {
            int number = Integer.parseInt(str);
            return number;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return -1;
        }
    }


    /**
     * 调整极值亮度
     * 1最暗 2最亮 3柔和
     *
     * @param
     */
    private void controlLd2(int ldType) {
        BallLight group = selectGroup;
        if (group == null) {
            speackOK(R.raw.select_group);
            myToast(sContext.getString(R.string.select_group_first), true);
            return;
        }
        int mode = group.getMode();
        int ld = 0;
        if (ldType == 1) {
            ld = 0;
        } else if (ldType == 2) {
            ld = 235;
        } else if (ldType == 3) {
            ld = 127;
        }

        if (mode == 1) {
            group.setLd1(ld);
//            controlRGB(group, "80");
            controlRGB(group, "00");
            speackOK(R.raw.ok);
        } else if (mode == 2) {
            group.setLd1(ld);
            controlSW(group, "80");
            speackOK(R.raw.ok);
        }
        mBallLightDao.updateBallLight(group);
        selectGroup = group;
        if (selectGroup.getFamilyId().equals("12345678")) {
            mDefaultBallLight = group;
        } else if (!selectGroup.getFamilyId().equals("12345678") && selectGroup.getGroupId().equals("00")) {
            mAllGroup = selectGroup;
        }
        LogHelper.i("AppManager.selectGroup=" + selectGroup.toString());
    }

    /**
     * 中文
     * 调整亮度
     *
     * @param content
     * @param
     */
    private void controlLd(String content, int ldType) {
        BallLight group = selectGroup;
        if (group == null) {
            speackOK(R.raw.select_group);
            myToast(sContext.getString(R.string.select_group_first), true);
            return;
        }
        int mode = group.getMode();

        int ld = (mode == 1) ? group.getLd1() : group.getLd1();

        if (content.contains("liang du jian") || content.contains("liang du di")
                || content.contains("liang du gan") || content.contains("liang du qiang")
                || content.contains("niang du jian") || content.contains("niang du di")
                || content.contains("niang du gan")
                || content.contains("lang du jian") || content.contains("lang du di")
                || content.contains("lang du gan")
                || content.contains("an yi ｄ")
                || content.contains("an yi di") || content.contains("yan yi di")
                || content.contains("yan １ dian") || content.contains("ying yi dian")
                || content.contains("an yi dian") || content.contains("an dian")
                || content.contains("zai an") || content.contains("geng an")
                || content.contains("bian an") || content.contains("hun dian")
                || content.contains("hun yi dian") || content.contains("zai hun")
                || content.contains("geng hun") || content.contains("bian hun")
                || content.contains("liang du bian di")) {//亮度减小、亮度减少、暗一点
            ld = ld - 50;
            if (ld <= 0) ld = 0;
            if (mode == 1) group.setLd1(ld);
            else group.setLd1(ld);
            mBallLightDao.updateBallLight(group);
            String cmd;
            if (mode == 1) {
//                controlRGB(group, "80");
                controlRGB(group, "00");
            } else {
                controlSW(group, "80");
            }
            speackOK(R.raw.ok);

        } else if (content.contains("liang du zeng") || content.contains("liang du gao")
                || content.contains("liang du jia") || content.contains("liang du shen")
                || content.contains("liang yi ｄ") || content.contains("liang yi di")
                || content.contains("niang du zeng") || content.contains("niang du gao")
                || content.contains("niang du jia") || content.contains("niang du shen")
                || content.contains("niang yi ｄ") || content.contains("niang yi di")
                || content.contains("lang du zeng") || content.contains("lang du gao")
                || content.contains("lang du jia") || content.contains("lang du shen")
                || content.contains("lang yi ｄ") || content.contains("liang yi di")
                || content.contains("ming yi dian")
                || content.contains("liang dian") || content.contains("zai liang")
                || content.contains("geng liang") || content.contains("bian liang")
                || content.contains("niang dian") || content.contains("zai niang")
                || content.contains("geng niang") || content.contains("bian niang")
                || content.contains("lang dian") || content.contains("zai lang")
                || content.contains("geng lang") || content.contains("bian lang")


                ) {//亮度增、亮度高、亮度加、亮度升、亮一点、明一点、亮点、再亮、更亮、变亮、
            ld = ld + 50;
            if (ld > 235) ld = 235;

            if (mode == 1) group.setLd1(ld);
            else group.setLd1(ld);
            mBallLightDao.updateBallLight(group);
            String cmd;
            if (mode == 1) {
//                controlRGB(group, "80");
                controlRGB(group, "00");
            } else {
                controlSW(group, "80");
            }
            speackOK(R.raw.ok);


        }
        selectGroup = group;
        if (selectGroup.getFamilyId().equals("12345678")) {
            mDefaultBallLight = group;
        } else if (!selectGroup.getFamilyId().equals("12345678") && selectGroup.getGroupId().equals("00")) {
            mAllGroup = selectGroup;
        }
        LogHelper.i("AppManager.selectGroup=" + selectGroup.toString());

    }

    private String intToHex(int number) {
        String hex = Integer.toHexString(number);
        if ((hex.length() % 2) != 0) hex = "0" + hex;
        return hex;
    }


    private String numToHexs(int number) {
        String hex = Integer.toHexString(number).toUpperCase();
        if (hex.length() <= 2) {
            hex = hex.length() % 2 == 0 ? "00" + hex : "000" + hex;
        } else if (hex.length() >= 3) {
            hex = hex.length() % 2 == 0 ? hex : "0" + hex;
        }
        return hex;
    }


    /**
     * @param tag   代表调节的类型 1代表调节的是亮度  2代表调节的是色温
     * @param value 当调节的类型为1时候 填入的value为50的时候代表亮一点，输入的value为-50的时候代表暗一点 当输入的值为235的时候代表亮度最亮，当输入的值为-235代表当前的亮度最暗
     *              当调节的类型为2时候 填入的value为50的时候代表黄一点，输入的value为-50的时候代表白一点 当输入的值为255的时候代表最黄，当输入的值为-255代表当前的最白
     */
    private void sendPWMValue(int tag, int value) {
        int ld = mSelectDeviceCategory.getLd();
        int sw = mSelectDeviceCategory.getSw();
        DeviceCategoryDao deviceCategoryDao = new DeviceCategoryDao(sContext);

        if (tag == 1) {//调亮度
            ld += value;
            if (value > 0) {
                ld = ld > 235 ? 235 : ld;
            } else {
                ld = ld < 0 ? 0 : ld;
            }
            mSelectDeviceCategory.setLd(ld);
            ld += 20;
        } else if (tag == 2) {//调色温
            sw += value;
            if (value > 0) {
                sw = sw > 255 ? 255 : sw;
            } else {
                sw = sw < 0 ? 0 : sw;
            }
            ld = ld + 20;
            mSelectDeviceCategory.setSw(sw);
        }
        deviceCategoryDao.updateDeviceCategory(mSelectDeviceCategory);

        int pwm1 = 255;
        int pwm2 = 255;

        if (sw <= 127) {
            pwm1 = sw * 2;
        } else {
            pwm2 = (255 - sw) * 2;
        }

        pwm1 = ld * pwm1 / 255;
        pwm2 = ld * pwm2 / 255;

        pwm1 = pwm1 > 255 ? 255 : pwm1;
        pwm2 = pwm2 > 255 ? 255 : pwm2;

        String data1 = numToHex(pwm1) + numToHex(pwm2) + "0000";
        commandAdvertise("02", mSelectDeviceCategory.getFamilyId(), mSelectDeviceCategory.getGroupId(), data1, "000000", mCurrFamily.getSeed(), mCurrFamily.getFeedback(), 1, "7203");

    }


/************************************新协议******************************************/
    /**
     * 通知删除组
     *
     * @param group
     */
    public void notifyDeleteGroup(BallLight group) {
        int seed = mCurrFamily.getSeed();
        int feedback = mCurrFamily.getFeedback();
        if (group.getFamilyId().equalsIgnoreCase("12345678")) {
            seed = 73;
            feedback = 4777;
        }
        String cmdData = new StringBuilder().append(group.getFamilyId()).append(group.getGroupId()).append("00000000").append("000000").toString();
        AdvertiserManager.getInstance().isBleAdvertise = true;
        commandCmd("06", cmdData, seed, feedback);
        AdvertiserManager.getInstance().isBleAdvertise = false;
    }

    /**
     * 全关或者全关命令
     *
     * @param cmd:传入"000100"代表开，传入"000000"代表关
     */
    public void allPower(String groupid, String cmd) {
        String familyId = mCurrFamily.getFamilyId();
        int seed = mCurrFamily.getSeed();
        int feedback = mCurrFamily.getFeedback();
      /*  if (mSelectDeviceCategory.getFamilyId().equalsIgnoreCase("12345678")) {
            familyId = "12345678";
            seed = 73;
            feedback = 4777;
        }*/
        isBLControlAll = true;
        commandCmd("02", familyId, groupid, "00000000", cmd, seed, feedback);
        isBLControlAll = false;
    }

    /**
     * 关某一组灯
     *
     * @param groupId 组号
     * @param cmd     传入"000100"代表开，传入"000000"代表关
     */
    public void onOrOff(String groupId, String cmd) {
        String familyId = mCurrFamily.getFamilyId();
        int seed = mCurrFamily.getSeed();
        int feedback = mCurrFamily.getFeedback();
        if (mSelectDeviceCategory.getFamilyId().equalsIgnoreCase("12345678")) {
            familyId = "12345678";
            seed = 73;
            feedback = 4777;
        }
        commandCmd("02", familyId, groupId, "00000000", cmd, seed, feedback);
    }


    protected void controlNeutral() {
        BallLight group = selectGroup;
        if (group == null) return;
        group.setSw(127);
        group.setMode(2);
        controlSW(group, "00");
        mBallLightDao.updateBallLight(group);
        selectGroup = group;
    }


    /**
     * @param option 00-顔色  80-亮度
     */
    public void controlRGB(BallLight group, String option) {
        String cmd = "01" + option + "00";
        String rgbHex = group.getRgb();
        int bright = group.getLd1() + 20;
        if (bright > 255) {
            bright = 255;
        }
        String ldHex = intToHex(bright);
        String sceneData = group.getFamilyId() + group.getGroupId() + rgbHex + "01" + ldHex + ldHex + "00";

        int seed = mCurrFamily.getSeed();
        int feedback = mCurrFamily.getFeedback();
        if (group.getFamilyId().equalsIgnoreCase("12345678")) {
            seed = 73;
            feedback = 4777;
            mDefaultBallLight = group;
        } else {

            if (group.getGroupId().equals("00")) {
                List<BallLight> allBallLight = mBallLightDao.getAllBallLight();
                for (BallLight group1 : allBallLight) {
                    group1.setMode(group.getMode());
                    group1.setLd1(group.getLd1());
                    group1.setRgb(rgbHex);
                    String sceneData2 = group1.getFamilyId() + group1.getGroupId() + rgbHex + "01" + ldHex + ldHex + "00";
                    group1.setScenedata(sceneData2);
                    mBallLightDao.updateBallLight(group1);
                }
            } else {
                group.setScenedata(sceneData);
                mBallLightDao.updateBallLight(group);
            }


        }

        commandCmd("03", group.getFamilyId(), group.getGroupId(), rgbHex + ldHex, cmd, seed, feedback);
    }


    /**
     * 专门针对滑条处理
     *
     * @param option 00-顔色  80-亮度
     */
    public void controlSeekBarRGB(BallLight group, String option) {
        String cmd = "01" + option + "00";
        String rgbHex = group.getRgb();
        int bright = group.getLd1() + 20;
        if (bright > 255) {
            bright = 255;
        }
        String ldHex = intToHex(bright);
        String sceneData = group.getFamilyId() + group.getGroupId() + rgbHex + "01" + ldHex + ldHex + "00";

        int seed = mCurrFamily.getSeed();
        int feedback = mCurrFamily.getFeedback();
        if (group.getFamilyId().equalsIgnoreCase("12345678")) {
            seed = 73;
            feedback = 4777;
            mDefaultBallLight = group;
        } else {
            if (isStopTouch) {
                if (group.getGroupId().equals("00")) {
                    List<BallLight> allBallLight = mBallLightDao.getAllBallLight();
                    for (BallLight group1 : allBallLight) {
                        group1.setMode(group.getMode());
                        group1.setLd1(group.getLd1());
                        group1.setRgb(rgbHex);
                        String sceneData2 = group1.getFamilyId() + group1.getGroupId() + rgbHex + "01" + ldHex + ldHex + "00";
                        group1.setScenedata(sceneData2);

                        mBallLightDao.updateBallLight(group1);
                    }
                    mAllGroup = group;
                } else {
                    group.setScenedata(sceneData);
                    mBallLightDao.updateBallLight(group);

                }
            }

        }

        commandCmd("03", group.getFamilyId(), group.getGroupId(), rgbHex + ldHex, cmd, seed, feedback);
    }


    /**
     * 专门针对滑条处理
     *
     * @param option 00-顔色  80-亮度
     */
    public void controlSeekBarSW(BallLight group, String option) {
        String cmd = "00" + option + "00";

        int sw = group.getSw();
        int red = sw;
        int green = 255 - sw;
        int blue = 0;
        int bright = group.getLd1() + 20;
        if (bright > 255) {
            bright = 255;
        }
        String swHexs = intToHex(red) + intToHex(green) + intToHex(blue);
        String ldHex = intToHex(bright);
        String sceneData = group.getFamilyId() + group.getGroupId() + swHexs + "00" + ldHex + ldHex + "00";


        int seed = mCurrFamily.getSeed();
        int feedback = mCurrFamily.getFeedback();
        if (group.getFamilyId().equalsIgnoreCase("12345678")) {
            seed = 73;
            feedback = 4777;
            mDefaultBallLight = group;
        } else {
            if (isStopTouch) {
                if (group.getGroupId().equals("00")) {
                    List<BallLight> allBallLight = mBallLightDao.getAllBallLight();
                    for (BallLight group1 : allBallLight) {
                        group1.setMode(group.getMode());
                        group1.setLd1(group.getLd1());
                        group1.setSw(sw);
                        String sceneData2 = group1.getFamilyId() + group1.getGroupId() + swHexs + "00" + ldHex + ldHex + "00";
                        group1.setScenedata(sceneData2);
                        mBallLightDao.updateBallLight(group1);

                    }
                    mAllGroup = group;
                } else {
                    group.setScenedata(sceneData);
                    mBallLightDao.updateBallLight(group);
                }

            }
        }

        commandCmd("03", group.getFamilyId(), group.getGroupId(), swHexs + ldHex, cmd, seed, feedback);
    }


    /**
     * @param option 00-顔色  80-亮度
     */
    public void controlSW(BallLight group, String option) {
        String cmd = "00" + option + "00";

        int sw = group.getSw();
        int red = sw;
        int green = 255 - sw;
        int blue = 0;
        int bright = group.getLd1() + 20;
        if (bright > 255) {
            bright = 255;
        }
        String swHexs = intToHex(red) + intToHex(green) + intToHex(blue);
        String ldHex = intToHex(bright);
        String sceneData = group.getFamilyId() + group.getGroupId() + swHexs + "00" + ldHex + ldHex + "00";
        group.setScenedata(sceneData);

        int seed = mCurrFamily.getSeed();
        int feedback = mCurrFamily.getFeedback();
        if (group.getFamilyId().equalsIgnoreCase("12345678")) {
            seed = 73;
            feedback = 4777;
            mDefaultBallLight = group;
        } else {
            if (group.getGroupId().equals("00")) {
                List<BallLight> allBallLight = mBallLightDao.getAllBallLight();
                for (BallLight group1 : allBallLight) {
                    group1.setMode(group.getMode());
                    group1.setLd1(group.getLd1());
                    group1.setSw(sw);
                    String sceneData2 = group1.getFamilyId() + group1.getGroupId() + swHexs + "00" + ldHex + ldHex + "00";
                    group1.setScenedata(sceneData2);
                    mBallLightDao.updateBallLight(group1);
                }
                mAllGroup = group;
            } else {
                mBallLightDao.updateBallLight(group);
            }
        }

        commandCmd("03", group.getFamilyId(), group.getGroupId(), swHexs + ldHex, cmd, seed, feedback);
    }


    /**
     * @param group 当前组号
     * @param data1 4个字节数据
     * @param data2 3个字节数据
     */

    public void controlScene(BallLight group, String data1, String data2) {
        Log.i(TAG, "controlScene: 情景控制");
        String scenedata = group.getFamilyId() + group.getGroupId() + data1 + data2;
        group.setScenedata(scenedata);
        int seed = mCurrFamily.getSeed();
        int feedback = mCurrFamily.getFeedback();
        if (group.getFamilyId().equalsIgnoreCase("12345678")) {
            seed = 73;
            feedback = 4777;

            mDefaultBallLight = group;
        } else {
            if (group.getGroupId().equals("00")) {
                List<BallLight> allBallLight = mBallLightDao.getAllBallLight();
                for (BallLight group1 : allBallLight) {
                    String scenedata2 = group1.getFamilyId() + group1.getGroupId() + data1 + data2;
                    group1.setScenedata(scenedata2);
                    group1.setRgb(group.getRgb());
                    group1.setSw(group.getSw());
                    group1.setMode(group.getMode());
                    group1.setLd1(group.getLd1());
                    mBallLightDao.updateBallLight(group1);
                }
                mAllGroup = group;
            } else {
                mBallLightDao.updateBallLight(group);
            }

        }

        commandCmd("80", scenedata, seed, feedback);

    }


    private void onScene(String scenedata) {
//        commandSceneCmd("80", scenedata, mCurrFamily.getSeed(), mCurrFamily.getFeedback());
    }


    public void sendDelayedMsg(int what, int delayed) {
        if (mHandler.hasMessages(what))
            mHandler.removeMessages(what);
        mHandler.sendEmptyMessageDelayed(what, delayed);
    }

    private static final int HANDLER_SECNE_POWER = 0x21;


    private MyHandler mHandler = new MyHandler(sContext);


    private static class MyHandler extends Handler {
        private WeakReference<Context> reference;

        MyHandler(Context context) {
            reference = new WeakReference<>(context);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            BaseApp activity = (BaseApp) reference.get();
            switch (msg.what) {
                case HANDLER_SECNE_POWER:
                    AppManager.getInstance(activity).secnePower();
                    break;
            }
        }

    }

    /************************************新协议******************************************/

    public void commandCmd(String type, String familyId, String groupId, String data1, String data2, int seed, int feadback) {
        mIsSceneCMD = false;
        cancelHandleTask(HANDLER_SECNE_POWER);
        StringBuilder builder = new StringBuilder();
        builder.append(type).append(familyId).append(groupId).append(data1).append(data2);
        String data = builder.toString();
        //加密数据
        String encryptdata = EncryUtil.encrypt(data, seed, feadback, "7201");
        mAdvertiserManager.startAdvertise(encryptdata, 20, 1, "7201");
        passthroughData2(encryptdata);
    }


    public void commandCmd(String type, String scenedata, int seed, int feadback) {
        mIsSceneCMD = false;
        cancelHandleTask(HANDLER_SECNE_POWER);
        StringBuilder builder = new StringBuilder();
        builder.append(type).append(scenedata);
        String data = builder.toString();
        //加密数据
        String encryptdata = EncryUtil.encrypt(data, seed, feadback, "7201");
        mAdvertiserManager.startAdvertise(encryptdata, 20, 1, "7201");
        passthroughData2(encryptdata);
    }

    /**
     * 专门针对遥控分享的发送方法
     *
     * @param type
     * @param scenedata
     * @param seed
     * @param feadback
     */
    public void commandCmdShare(String type, String scenedata, int seed, int feadback, String deviceType) {
        mIsSceneCMD = false;
        cancelHandleTask(HANDLER_SECNE_POWER);
        StringBuilder builder = new StringBuilder();
        builder.append(type).append(scenedata);
        String data = builder.toString();
        //加密数据
        String encryptdata = EncryUtil.encrypt(data, seed, feadback, deviceType);
        mAdvertiserManager.startAdvertise(encryptdata, 20, 1, deviceType);
//        mAdvertiserManager.startAdvertise(data,20,0,"7201");
//        mAdvertiserManager.startAdvertise(data,20,1,"7201");
    }

    public void commandSceneCmd(String type, String familyId, String groupId, String data1, String data2, int seed, int feadback) {

        StringBuilder builder = new StringBuilder();
        builder.append(type).append(familyId).append(groupId).append(data1).append(data2);
        String data = builder.toString();
        if (familyId.equalsIgnoreCase("12345678")) {
            seed = 73;
            feadback = 4777;
        }
        //加密数据
        String encryptdata = EncryUtil.encrypt(data, seed, feadback, "7201");
        mAdvertiserManager.startAdvertise(encryptdata, 20, 1, "7201");
        passthroughData2(encryptdata);

    }


   /* public void commandSceneCmd(String type, String scenedata, int seed, int feadback) {

        StringBuilder builder = new StringBuilder();
        builder.append(type).append(scenedata);
        String data = builder.toString();
        //加密数据
        String encryptdata = EncryUtil.encrypt(data, seed, feadback, "7201");
        mAdvertiserManager.startAdvertise(encryptdata, 20, 1, "7201");

    }*/

    /**
     * @param type          命令类型
     * @param familyId      家庭码
     * @param groupId       组号
     * @param data1         四个字节
     * @param data2         三个字节
     * @param seed          加密种子
     * @param feedback      反馈参数
     * @param advertiseType 发送的广播类型
     * @param deviceType    控制的设备类型
     */
    public void commandAdvertise(String type, String familyId, String groupId, String data1, String data2,
                                 int seed, int feedback, int advertiseType, String deviceType) {
        StringBuilder builder = new StringBuilder();
        builder.append(type).append(familyId).append(groupId).append(data1).append(data2);
        String data = builder.toString();
        //加密数据
        if (familyId.equalsIgnoreCase("12345678")) {
            seed = 73;
            feedback = 4777;
        }

        String encryptdata = EncryUtil.encrypt(data, seed, feedback, deviceType);
        mAdvertiserManager.startAdvertise(encryptdata, 20, advertiseType, deviceType);


        passthroughData2(encryptdata);
    }


    /**
     * 可以设置发送包数的命令
     *
     * @param type            命令类型
     * @param familyId        家庭码
     * @param groupId         组号
     * @param data1           四个字节
     * @param data2           三个字节
     * @param seed            加密种子
     * @param feadback        反馈参数
     * @param advertiseType   发送的广播类型
     * @param deviceType      控制的设备类型
     * @param advertiseNumber 发送的包数
     */
    public void commandAdvertiseInNum(String type, String familyId, String groupId, String data1, String data2,
                                      int seed, int feadback, int advertiseType, String deviceType, int advertiseNumber) {
        StringBuilder builder = new StringBuilder();
        builder.append(type).append(familyId).append(groupId).append(data1).append(data2);
        String data = builder.toString();
        //加密数据
        String encryptdata = EncryUtil.encrypt(data, seed, feadback, deviceType);
        mAdvertiserManager.startAdvertise(encryptdata, advertiseNumber, advertiseType, deviceType);
    }

    public void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
        }
    }

    //网关操作
    public String getLightSerial() {
        String key = "BALLLIGHT_SERIAL";
        int serial_number = com.newenergy.light.Helper.SPUtil.getInteger(key, 1);
        serial_number--;
        if (serial_number < 1) {
            serial_number = 1;
        } else if (serial_number > 4095) {
            serial_number = 1;
        }
        String serial = BleDataHelper.numberToSerial(serial_number);
        return serial;
    }

    //   执行透传数据
    public void passthroughData2(String sendData) {
        boolean network = BLCommonUtils.checkNetwork(sContext);
        if (!network) {
            Log.i(TAG, "passthroughData2: 透传失败:网络不可用");
            return;
        }
        boolean isLogin = BLLocalFamilyManager.getInstance().isLogin;
        if (!isLogin) {
            Log.e(TAG, "passthroughData2: 透传失败:没登录");
            return;
        }
        Log.i(TAG, "passthroughData2");
        String did = null;
        if (isControlAll || isBLControlAll) {
            if (mBLAllDeviceInfo == null) {
                Log.e(TAG, "passthroughData2: mDid为空");
                return;
            }
            did = mBLAllDeviceInfo.getDid();
        } else {
            //取出当前设备的bldid
            did = getCurrDeviceCategoryBLDid();
        }
        if (TextUtils.isEmpty(did)) {
            Log.e(TAG, "passthroughData2: mDid为空");
            return;
        }
        mDid = did;

        int length = sendData.length();
        if (length != 26) {
            toast(length + "!= 26");
            return;
        }
        sendData = "0e00" + sendData;

        String headerHex = "A5A55A5A";
        String allSum = "FFFF";
        String totalLenHex = "FFFF";
        String versionHex = "0000";
        String didHex = did.substring(16, 32);
        //mPassCmdType普通命令00   对码命令 FO
        String cmdHex = mPassCmdType;
        String opHex = "00";
//        String seqHex = mLightSerial;
        String seqHex = getLightSerial();
        Log.i(TAG, "执行透传数据: seqHex =  " + seqHex);
        String repHex = "06";
        String type = "00";
//        int len = (sendData.length()) / 2 + 1;
//        String lenHex = BleDataHelper.numberToHex(len);
        String allData = sendData;
        String dataSum = getDataSumHexStr(allData, 2);
        int totalLen = 2 + 8 + 5 + (allData.length() / 2) + 2;
        totalLenHex = BleDataHelper.numberToSerial(totalLen);
        //用于计算总累加和
        String allSumData = totalLenHex + versionHex + didHex + cmdHex + opHex +
                seqHex + repHex + allData + dataSum;

        //计算总累加和
        allSum = getDataSumHexStr(allSumData, 2);
        String cmdData = headerHex + allSum + totalLenHex + versionHex + didHex +
                cmdHex + opHex + seqHex + repHex + allData + dataSum;

        byte[] datas = BleDataHelper.StrToBytes(cmdData);

        Log.i(TAG, "passthroughData: cmdData = " + BleDataHelper.bytesToHexString(datas, 1000));
        mPassCmdType = "00";
        passDnaData(datas);

    }

    /*** 获取当前灯的 bldid */
    private String getCurrDeviceCategoryBLDid() {
        if (mSelectDeviceCategory == null) {
            return null;
        }
        String blDid = mSelectDeviceCategory.getBlDid();
        if (!TextUtils.isEmpty(blDid)) {
            return blDid;
        }
        DeviceCategoryDao categoryDao = new DeviceCategoryDao(sContext);
        DeviceCategory deviceCategory = categoryDao.getByUuid(mSelectDeviceCategory.getUuid());
        if (deviceCategory == null || TextUtils.isEmpty(deviceCategory.getBlDid())) {
            return null;
        }
        blDid = deviceCategory.getBlDid();
        mSelectDeviceCategory.setBlDid(blDid);
        return blDid;
    }

    public void passDnaData(final byte[] datas) {
        MyThreadManager.getInstance().doTask(() -> {
            BLPassthroughResult result = BLLet.Controller.dnaPassthrough(mDid, null, datas);
            Log.i(TAG, "passthroughData: result   succeed = " + result.succeed() + "  error = " + result.getError()
                    + "   msg=" + result.getMsg());
            if (result.succeed()) {
                String hexStr = BleDataHelper.bytesToHexString(result.getData(), 100);
                Log.i(TAG, "透传返回数据 data = " + hexStr);
            }
        });
    }

    public String getDataSumHexStr(String dataStr, int len) {
        int sum = 0;
        int length = dataStr.length();
        if (length < 2 || (length % 2 != 0)) {
            toast("getDataSumHexStr 长度不是偶数：" + length);
//            Log.i( TAG,"getDataSumHexStr 错误：%zd != 2 , dataSt= %@", length, dataStr);
            return null;
        }
        for (int i = 0; i < length; i += 2) {
            String data1 = dataStr.substring(i, i + 2);
            sum += Integer.parseInt(data1, 16);
        }
        String sumHexStr = BleDataHelper.numberToHex(sum);
        len *= 2;
        int sumHexLen = sumHexStr.length();
        int loc = sumHexLen - len;
        if (sumHexLen == len) {
            return sumHexStr;
        } else if (sumHexLen > len) {
            sumHexStr = sumHexStr.substring(loc, loc + len);
        } else {//长度不够，前面拼接字符串（补0）
            loc = 0 - loc;
            String str2 = "";
            for (int i = 0; i < loc; i++) {
                str2 += "0";
            }
            sumHexStr = str2 + sumHexStr;
        }
//        Log.i(TAG, "getDataSumHexStr: sumHexStr = " + sumHexStr);
        return sumHexStr;
    }

    private void toast(String msg) {
        if (mToast == null) {
            mToast = Toast.makeText(sContext, "", Toast.LENGTH_SHORT);
        }
        mToast.setText(msg);
        mToast.show();

    }
}
